Both sides previous revisionPrevious revisionNext revision | Previous revision |
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:digital_io [2023/07/07 22:38] – pczekalski | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:digital_io [2023/11/23 10:21] (current) – pczekalski |
---|
==== Digital ports, reading inputs, outputting data === | ====== Digital ports, reading inputs, outputting data ====== |
| {{:en:iot-open:czapka_b.png?50| General audience classification icon }}{{:en:iot-open:czapka_m.png?50| General audience classification icon }}{{:en:iot-open:czapka_e.png?50| General audience classification icon }}\\ |
| Every microcontroller has many pins that can be used to connect external electronic elements. In the examples shown in previous chapters, LED was used. Such LED can be connected to a chosen General Purpose Input Output (GPIO) pin and can be controlled by setting a HIGH or LOW state. Below are some details of the functions that allow the manipulation of GPIOs using the Arduino framework. In the next chapter, analogue signals will be considered. |
| |
Every microcontroller has a number of pins that can be used to connect external electronic elements. In the examples shown in previous chapters, LED was used. Such LED can be connected to chosen General Purpose Input Output (GPIO) pin and can be controlled by setting a HIGH or LOW state on the pin. Below are some details of the set of functions that allow to manipulate GPIOs. In the next chapter, analogue signals will be considered. | ==== Digital I/O ==== |
| Microcontrollers' digital inputs and outputs allow for connecting different sensors and actuators to the board. Digital signals can take two values – //HIGH//(1) or //LOW//(0). These states correspond to high voltage (usually corresponding to the power supply voltage of the microcontroller) and low voltage (around 0V). These inputs and outputs are used in applications when the signal can have only two states. |
=== Digital I/O === | |
Microcontrollers' digital inputs and outputs allow for connecting different types of sensors and actuators to the board. Digital signals can take two values – //HIGH//(1) or //LOW//(0). These states correspond to high voltage (usually corresponding to the power supply voltage of the microcontroller) and low voltage (around 0V). These types of inputs and outputs are used in applications when the signal can have only two states. | |
| |
<note important> | <note important> |
Notice that the voltage that the microcontroller is powered with can be different (usually lower) than the voltage provided directly to the board. For example, the ATmega microcontroller on the Arduino Uno board is powered with 5V, while the board itself can be powered from an external source providing 7-12V. Other microcontrollers require different voltages, e.g. Espressif 3.3V. Refer to the device manual for a valid range of voltages. | Notice that the voltage the microcontroller is powered with can be different (usually lower) than the voltage provided directly to the board. For example, the ATmega microcontroller on the Arduino Uno board is powered with 5V, while the board itself can be powered from an external source providing 7-12V. Other microcontrollers require different voltages, e.g. Espressif 3.3V. Refer to the device manual for a valid range of voltages. |
</note> | </note> |
| |
The parameter //pin// is the number of the pin. | 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 turns on the internal pull-up resistor connected between the power supply and the pin itself. It ensures that if the pin remains unconnected, the logic state will be stable and equal to HIGH. More about pull-up resistors can be found on the Arduino homepage ((https://www.arduino.cc/en/Tutorial/DigitalPins)). | 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 turns on the internal pull-up resistor between the power supply and the pin itself. It ensures that if the pin remains unconnected, the logic state will be stable and equal to HIGH. More about pull-up resistors can be found on the Arduino homepage ((https://www.arduino.cc/en/Tutorial/DigitalPins)). |
| |
<note important>Majority of the MCUs are quite flexible in the configuration and use of GPIOS. There are some special GPIOs, however. Some of them cannot work as inputs or outputs or do not have internal pull-up resistors to enable them. Refer to the technical documentation of the hardware you use before setting up your configuration and external devices like sensors, buttons and applications. Some of the GPIOs are also predefined for communication protocols such as Serial, I2C, SPI, etc.</note> | <note important> Most MCUs are pretty flexible in the configuration and use of GPIOS. There are some special GPIOs, however. Some of them cannot work as inputs or outputs or do not have internal pull-up resistors to enable them. Refer to the technical documentation of the hardware you use before setting up your configuration and external devices like sensors, buttons and applications. Some of the GPIOs are also predefined for communication protocols such as Serial, I2C, SPI, etc.</note> |
| |
**digitalWrite()** | **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 LEDs. This function as well does not return any value. | The function //digitalWrite()// writes a //HIGH// or //LOW// value to the pin. This function is used for digital pins, such as turning on/off LEDs. This function also does not return any value. |
| |
The syntax of a function is the following: | The syntax of a function is the following: |
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 power supply voltage and //LOW// to 0 V. | 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 power supply voltage 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. | Using this function for pins set to have the INPUT mode is also possible. In this case, //HIGH// or //LOW// values enable or disable the internal pull-up resistor. |
| |
**digitalRead()** | **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 function //digitalRead()// works in the opposite direction than the function //digitalWrite()//. It reads the pin's value that can be either //HIGH// or //LOW// and returns it. |
| |
The syntax of a function is the following: | The syntax of a function is the following: |