This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| en:iot-open.eu:wp5:programming:analog_io [2023/06/25 17:49] – created ktokarz | en:iot-open.eu:wp5:programming:analog_io [2023/06/25 18:21] (current) – removed ktokarz | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ==== Maniulating analog signals ==== | ||
| - | 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 // | ||
| - | === Analog input === | ||
| - | For measuring the analog signal, microcontrollers have built-in **analog-to-digital converter** (ADC) that returns the digital value of the voltage level. Usually, it is the binary number that corresponds to the input voltage, not the value in Volts. The number of bits of the output value depends on the accuracy and internal construction of the converter and usually varies between 8 and 12. | ||
| - | |||
| - | **analogRead()** | ||
| - | |||
| - | The function // | ||
| - | |||
| - | The syntax of a function is the following: | ||
| - | <code c> | ||
| - | analogRead(pin); | ||
| - | </ | ||
| - | |||
| - | The parameter //pin// is the name of the pin whose value is read. | ||
| - | |||
| - | The return type of the function is the integer value. On the Arduino Uno boards it ranges between 0 and 1023. The reading of each analog input takes around 100 ms. | ||
| - | |||
| - | < | ||
| - | Not every pin can be used as analog input. Read the documentation of the chosen development board for details. | ||
| - | </ | ||
| - | |||
| - | === Analog output === | ||
| - | Unlike analog input, the analog output does not generate varying voltage directly on the pin. In general, it uses the technique known as (//Pulse Width Modulation// | ||
| - | |||
| - | **analogWrite()** | ||
| - | |||
| - | The function // | ||
| - | |||
| - | The syntax of a function is the following: | ||
| - | <code c> | ||
| - | analogWrite(pin, | ||
| - | </ | ||
| - | |||
| - | The parameter //pin// is the number of the pin. | ||
| - | |||
| - | The parameter //value// is the PWM signal value that can differ from 0 (off) to 255 (100% on). | ||
| - | |||
| - | This function does not have the return type. | ||
| - | |||
| - | < | ||
| - | Because PWM output is often generated by an internal timer, not every pin can be used as analog output. Read the documentation of the chosen development board for details. | ||
| - | </ | ||
| - | |||
| - | The following example shows reading an analog value from the A0 input of an Arduino Uno board and writing the analog value to the output that can control the intensity of the LED. | ||
| - | |||
| - | <code c> | ||
| - | #define LED_pin 3 //the pin number is chosen to support PWM generation | ||
| - | |||
| - | void setup() { | ||
| - | pinMode(LED_pin, | ||
| - | } | ||
| - | |||
| - | int value; | ||
| - | |||
| - | void loop() { | ||
| - | value = analogRead(A0); | ||
| - | value = value >> 2; //it should be converted to the value of the range 0 - 255 | ||
| - | analogWrite(LED_pin, | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | **Check Yourself** | ||
| - | |||
| - | 1. What command for analog input read is used? | ||
| - | |||
| - | 2. What technique is used to obtain an analog output signal? | ||