This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:analog_io [2023/07/08 18:33] – pczekalski | en:iot-open:introductiontoembeddedprogramming2:cppfundamentals:analog_io [2023/11/23 10:21] (current) – pczekalski | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ==== Manipulating analogue signals ==== | + | ====== Manipulating analogue signals ====== |
+ | {{: | ||
The analogue 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 // | The analogue 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 === | + | ==== Analog input ==== |
For measuring the analogue signal, microcontrollers have built-in **analogue-to-digital converter** (ADC) that returns the digital value of the voltage level. Usually, the binary number 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. | For measuring the analogue signal, microcontrollers have built-in **analogue-to-digital converter** (ADC) that returns the digital value of the voltage level. Usually, the binary number 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. | ||
Line 14: | Line 15: | ||
</ | </ | ||
- | The parameter //pin// is the name of the pin whose value is read. | + | The parameter //pin// is the pin's name 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 analogue input takes around 100 ms. | 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 analogue input takes around 100 ms. | ||
< | < | ||
- | Not every pin can be used as analogue input. Read the documentation of the chosen development board for details. | + | Not every pin can be used as an analogue input. Read the documentation of the chosen development board for details. |
</ | </ | ||
- | == A special note on analogue inputs in ESP32 == | + | ==== Analog output |
- | Some microcontrollers use specific setups. Analogue input may work out of the box. Still, low-level control usually brings better results and higher flexibility (i.e. instead of changing the input voltage to reflect the full measurement range, you can regulate internal amplification and sensitivity. Please note implementation varies even between ESP32 chips family and not all chips provide all of the functions so it is essential to refer to the technical documentation. | + | Unlike analogue input, the analogue output does not generate varying voltage directly on the pin. In general, it uses the technique known as (//Pulse Width Modulation// |
- | + | ||
- | ESP32 has 15 channels exposed (18 total) of the up to 12-bit resolution ADCs. Reading the raw data (12-bit resolution is the default, 8 samples per measure as default) using the '' | + | |
- | Technically, | + | |
- | Just execute '' | + | |
- | A number of useful functions are here (not limited to): | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | * '' | + | |
- | + | ||
- | <note important> | + | |
- | + | ||
- | === Analog output === | + | |
- | Unlike analogue input, the analogue output does not generate varying voltage directly on the pin. In general, it uses the technique known as (//Pulse Width Modulation// | + | |
**analogWrite()** | **analogWrite()** | ||
- | The function // | + | The function // |
The syntax of a function is the following: | The syntax of a function is the following: | ||
Line 65: | Line 48: | ||
<code c> | <code c> | ||
- | #define LED_pin 3 //the pin number is chosen to support PWM generation | + | #define LED_pin 3 //the pin number is chosen to support PWM generation |
void setup() { | void setup() { | ||
Line 71: | Line 54: | ||
} | } | ||
- | int value; | + | int value; |
void loop() { | void loop() { | ||
- | value = analogRead(A0); | + | value = analogRead(A0); |
- | value = value >> 2; //it should be converted to the value of the range 0 - 255 | + | value = value >> 2; //it should be converted to the value 0-255 |
analogWrite(LED_pin, | analogWrite(LED_pin, | ||
} | } | ||
</ | </ | ||
- | == A special note on ESP32 MCUs == | ||
- | PWM frequently controls analogue-style, | ||
- | The classical '' | ||
- | ESP32 has sixteen (0 to 15) PWM channels (controllers) that can be freely bound to any of the regular GPIOs.\\ ESP32 can use various " | ||
- | To use PWM in ESP32, one must perform the following steps: | ||
- | * configure GPIO pin as '' | ||
- | * initiate PWM controller by fixing PWM frequency and resolution, | ||
- | * bind the controller to the GPIO pin, | ||
- | * write to the controller (not to the PIN!) providing a duty cycle related to the resolution selected above - every call persistently sets the PWM duty cycle until there is the next call to the function setting duty cycle. |