Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
en:iot-open.eu:wp5:programming:digital_io [2023/06/25 17:47] ktokarzen:iot-open.eu:wp5:programming:digital_io [2023/06/25 18:22] (current) – removed ktokarz
Line 1: Line 1:
-==== Digital ports, reading inputs, outputting data === 
  
-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 === 
-The digital inputs and outputs of microcontrollers 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 (equal to the power supply voltage of the microcontroller), and low voltage (zero Volts). These types of inputs and outputs are used in applications when the signal can have only two states. 
- 
-<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. 
-</note> 
- 
-**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: 
-<code c> 
-pinMode(pin, mode); 
-</code> 
- 
-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)). 
- 
-**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 syntax of a function is the following: 
-<code c> 
-digitalWrite(pin, value); 
-</code> 
- 
-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 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. 
- 
-**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: 
-<code c> 
-digitalRead(pin); 
-</code> 
- 
-The parameter //pin// is the number of the pin. 
- 
-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. 
- 
-<code c> 
-#define BUTTON_pin 3 
-#define LED_pin 4 
- 
-void setup() { 
-  pinMode(LED_pin, OUTPUT); 
-  pinMode(BUTTON_pin, INPUT_PULLUP); 
-} 
- 
-bool state; 
- 
-void loop() { 
-  state = digitalRead(BUTTON_pin); //reading digital state of the input 
-  digitalWrite(LED_pin, state);    //writing state back to the output 
-} 
-</code> 
- 
-**Check Yourself** 
- 
-1. To assign the Arduino pin operation mode, which function is used? 
-  * Function //digitalWrite()//. 
-  * Function //pinMode()//. 
-  * Directive //#define// 
- 
-2. The digital output on the Arduino Uno board works as a power source with voltage? 
- 
-  * 5 V. 
-  * 12 V. 
-  * 3.3 V. 
en/iot-open.eu/wp5/programming/digital_io.1687715234.txt.gz · Last modified: 2023/06/25 14:47 (external edit)
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