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/11/17 16:52] – 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 22: | Line 23: | ||
</ | </ | ||
- | === Analog output === | + | ==== 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// | 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// | ||
Line 47: | 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 53: | 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, | ||
} | } |