Reading GPIOs, Outputting Data and Tracing

Digital I/O

The digital inputs and output of Arduino allow connecting different type of sensors and actuators to the board. Digital signals can take two values – HIGH(1) or LOW(0). These types of inputs and outputs are used in applications when the signal can have only two states.

pinMode()

The function pinMode() is essential to indicate whether the specified pin will behave like an input or an output. This function does not return any value. Usually, the mode of a pin is set in the setup() function of a program – only once on initialisation.

The syntax of a function is the following:

pinMode(pin, mode)

The parameter pin is the number of the pin.

The parameter mode can have three different values – INPUT, OUTPUT, INPUT_PULLUP depending on whether the pin will be used as an input or an output. The INPUT_PULLUP mode means that the pin will work as an input whose state will be inverted (set to the opposite). More about Pullup Resistors can be found in the Arduino homepage [1].

digitalWrite()

The function digitalWrite() writes a HIGH or LOW value to the pin. This function is used for digital pins, for example, to turn on/off LED. This function as well does not return any value.

The syntax of a function is the following:

digitalWrite(pin, value)

The parameter pin is the number of the pin. The parameter value can take values HIGH or LOW. If the mode of the pin is set to the OUTPUT, the HIGH sets voltage to +5 V and LOW – to 0 V.

It is also possible to use this function for pins that are set to have the INPUT mode. In this case, HIGH or LOW values enable or disable the internal pull-up resistor.

digitalRead()

The function digitalRead() works in the opposite direction than the function digitalWrite(). It reads the value of the pin that can be either HIGH or LOW and returns it.

The syntax of a function is the following:

digitalRead(pin)

The parameter pin is the number of the pin.

On the opposite as the functions viewed before, this one has the return type, and it can take a value of HIGH or LOW.

Analog I/O

The analogue inputs and outputs are used when the signal can take a range of values, unlike the digital signal that takes only two values (HIGH or LOW). For measuring the analogue signal, Arduino has built-in analogue-to-digital converter (ADC) that returns the digital value of the voltage level.

analogWrite()

The function analogWrite() is used to write an analog value (Pulse Width Modulation (PWM)) of the integer type as an output of the pin. The example of use is turning on/off the LED in various brightness levels or setting different speeds of the motors. The value that is written to the pin stays unchanged until the next value is written to the pin.

The syntax of a function is the following:

analogWrite(pin, value)

The parameter pin is the number of the pin.

The parameter value is the

This function does not have the return type.

analogRead()

The function analogRead() is used for analogue pins (A0, A1, A2, etc.) and it reads the value that is on the analogue pin.

The syntax of a function is the following:

analogRead(pin)

The parameter pin is the number of the pin whose value is read.

The return type of the function is the integer value between 0 and 1023. The reading of each analogue input takes around 100 ms that is 0.0001 s.

Check Yourself

1. To assign the Arduino pin operation mode, which function is using?

  • Function digitalWrite().
  • Function pinMode().
  • Directive #define.

2. What command for analogue input read is used?

3. The digital output on Arduino works as a power source with voltage?

  • 5 V.
  • 12 V.
  • 3.3 V.
en/iot-open/getting_familiar_with_your_hardware_rtu_itmo_sut/arduino_and_arduino_101_intel_curie/programming_fundamentals_rtu/interacting_with_hardware_devices_and_debugging_the_code.txt · Last modified: 2020/07/20 09:00 by 127.0.0.1
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0