Reading GPIOs, outputting data and debugging information

Digital I/O

The digital inputs and output of Arduino allow to connect 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 useful in applications when the signal can have only two states.

pinMode()

The function pinMode() is essential in order to indicate whether the the specified pin will behave as 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 initialization.

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 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 oposite). 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 +5V and LOW - to 0V.

It is also possible to use this function for pins that are set to have 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 analog 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 analog signal, Arduino has built-in analog-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 analog 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 microseconds that is 0.0001 second.

Check yourself

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

  • function digitalWrite()
  • function pinMode()
  • directive #define

2. What command for analog input read is used?

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

  • 5V
  • 12V
  • 3.3V
en/iot-open/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