This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:digital_io [2023/06/25 18:20] – created ktokarz | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:digital_io [2023/11/23 10:21] (current) – pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== Digital ports, reading inputs, outputting data === | + | ====== Digital ports, reading inputs, outputting data ====== |
+ | {{: | ||
+ | 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 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 some details of the set of functions that allow to manipulate GPIOs is shown. In the next chapter, analog signals will be considered. | + | ==== Digital I/O ==== |
- | + | Microcontrollers' | |
- | === Digital I/O === | + | |
- | The digital inputs and outputs | + | |
<note important> | <note important> | ||
- | Notice that the voltage | + | 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. |
</ | </ | ||
**pinMode()** | **pinMode()** | ||
- | The function // | + | The function // |
The syntax of a function is the following: | The syntax of a function is the following: | ||
Line 21: | Line 21: | ||
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//, // | + | The parameter //mode// can have three different values – //INPUT//, //OUTPUT//, // |
+ | |||
+ | <note important> | ||
**digitalWrite()** | **digitalWrite()** | ||
- | The function // | + | The function // |
The syntax of a function is the following: | The syntax of a function is the following: | ||
Line 35: | Line 37: | ||
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 // | + | The function // |
The syntax of a function is the following: | The syntax of a function is the following: | ||
Line 50: | Line 52: | ||
On the opposite of the functions viewed before, this one has the return type, and it can take a value of //HIGH// or //LOW//. | On the opposite of the functions viewed before, this one has the return type, and it can take a value of //HIGH// or //LOW//. | ||
- | In the code below the button connected to pin 3 controls the LED connected to pin 4. | + | In the code below, the button connected to pin 3 controls the LED connected to pin 4. |
<code c> | <code c> | ||
Line 69: | Line 71: | ||
</ | </ | ||
- | **Check Yourself** | ||
- | |||
- | 1. To assign the Arduino pin operation mode, which function is used? | ||
- | * Function // | ||
- | * Function // | ||
- | * Directive //# | ||
- | |||
- | 2. The digital output on the Arduino Uno board works as a power source with voltage? | ||
- | * 5 V. | ||
- | * 12 V. | ||
- | * 3.3 V. |