Chapter 9 AD/DA Converter
We have learned how to control the brightness of LED through PWM and understood that PWM is not the real analog before. In this chapter, we will learn how to read analog, convert it into digital and convert the digital into analog output. That is, ADC and DAC.
Project 9.1 Read the Voltage of Potentiometer
In this project, we will use the ADC function of ESP32 to read the voltage value of potentiometer. And then output the voltage value through the DAC to control the brightness of LED.
Component List
ESP32-WROVER x1
|
GPIO Extension Board x1
|
||
Breadboard x1
|
|||
LED x1
|
Resistor 220Ω x1
|
Jumper M/M x5
|
Rotary potentiometer x1
|
Component knowledge
Potentiometer
Potentiometer is a resistive element with three Terminal parts. Unlike the resistors that we have used thus far in our project which have a fixed resistance value, the resistance value of a potentiometer can be adjusted. A potentiometer is often made up by a resistive substance (a wire or carbon element) and movable contact brush. When the brush moves along the resistor element, there will be a change in the resistance of the potentiometer’s output side (3) (or change in the voltage of the circuit that is is a part). The illustration below represents a linear sliding potentiometer and its electronic symbol on the right.
What between potentiometer pin 1 and pin 2 is the resistor body, and pins 3 is connected to brush. When brush moves from pin 1 to pin 2, the resistance between pin 1 and pin 3 will increase up to body resistance linearly, and the resistance between pin 2 and pin 3 will decrease down to 0 linearly.
In the circuit. The both sides of resistance body are often connected to the positive and negative electrode of the power. When you slide the brush pin 3, you can get a certain voltage in the range of the power supply.
Rotary potentiometer
Rotary potentiometer and linear potentiometer have similar function; their only difference is: the resistance is adjusted by rotating the potentiometer.
Circuit
Schematic diagram |
|
Hardware connection |
If you need any support, please contact us via: support@freenove.com
|
Code
Move the program folder “Freenove_Ultimate_Starter_Kit_for_ESP32/Python/Python_Codes” to disk(D) in advance with the path of “D:/Micropython_Codes”.
Open “Thonny”, click “This computer” -> “D:” -> “Micropython_Codes” -> “09.1_AnalogRead and then click “AnalogRead.py”.
09.1_AnalogRead
Click “Run current script” and observe the message printed in “Shell”.
LEDs display as below:
“Shell” prints ADC value, DAC value, the output voltage of potentiometer and other information. In the code, we make the output voltage of the DAC pin equal to the input voltage of the ADC pin. Rotate the handle of the potentiometer, the printed information will change. When the voltage is greater than 1.6V (turn-on voltage of red LED), the LED starts to emit light. If you continue to increase the output voltage, the LED will gradually become brighter. And when the voltage is less than 1.6V, the LED will not light up, because this does not reach the turn-on voltage of the LED, which indirectly proves the difference between DAC and PWM. (If you have an oscilloscope, you can view the waveform output by the DAC through the oscilloscope)
The following is the code:
1from machine import ADC,Pin,DAC
2import time
3
4adc=ADC(Pin(36))
5adc.atten(ADC.ATTN_11DB)
6adc.width(ADC.WIDTH_12BIT)
7dac =DAC(Pin(25))
8try:
9 while True:
10 adcVal=adc.read()
11 dacVal=adcVal//16
12 voltage = adcVal / 4095.0 * 3.3
13 dac.write(dacVal)
14 print("ADC Val:",adcVal,"\tDACVal:",dacVal,"\tVoltage:",voltage,"V")
15 time.sleep_ms(100)
16except:
17 adc.deinit()
18
19
20# Please note that if an error occurs when running the code as follows, please press the RST button on the board.
21# The DAC can only be initialized in the firmware and cannot be disabled. Therefore, using it for the second time will cause an abnormality.
22# MPY: soft reboot
23# Traceback (most recent call last):
24# File "<stdin>", line 7, in <module>
25# OSError: (-259, 'ESP_ERR_INVALID_STATE')
Import Pin, ADC and DAC modules.
1from machine import ADC,Pin,DAC
2import time
Turn on and configure the ADC with the range of 0-3.3V and the data width of 12-bit data width, and turn on the DAC pin.
1adc=ADC(Pin(36))
2adc.atten(ADC.ATTN_11DB)
3adc.width(ADC.WIDTH_12BIT)
4dac =DAC(Pin(25))
Read ADC value once every 100 millisecods, convert ADC value to DAC value and output it, control the brightness of LED and print these data to “Shell”.
1while True:
2 adcVal=adc.read()
3 dacVal=adcVal//16
4 voltage = adcVal / 4095.0 * 3.3
5 dac.write(dacVal)
6 print("ADC Val:",adcVal,"\tDACVal:",dacVal,"\tVoltage:",voltage,"V")
7 time.sleep_ms(100)
Reference
- Class ADC
Before each use of ACD module, please add the statement “from machine import ADC” to the top of the python file.
machine.ADC(pin): Create an ADC object associated with the given pin.
pin: Available pins are: Pin(36)、Pin(39)、Pin(34)、Pin(35)、Pin(32)、Pin(33)。
ADC.read(): Read ADC and return the value.
ADC.atten(db): Set attenuation ration (that is, the full range voltage, such as the voltage of 11db full range is 3.3V)
db: attenuation ratio
ADC.ATTIN_0DB —full range of 1.2V
ADC.ATTN_2_5_DB —full range of 1.5V
ADC.ATTN_6DB —full range of 2.0 V
ADC.ATTN_11DB —full range of 3.3V
ADC.width(bit): Set data width.
bit: data bit
ADC.WIDTH_9BIT —9 data width
ADC.WIDTH_10BIT — 10 data width
ADC.WIDTH_11BIT — 11 data width
ADC.WIDTH_12BIT — 12 data width
- Class DAC
Before each use of DAC module, please add the statement “ from machine import DAC “ to the top of the python file.
machine.DAC(pin): Create a DAC object associated with the given pin.
pin: Available pins are: Pin(25)、Pin(26)
DAC.write(value): Output voltage
value: The range of data value: 0-255, corresponding output voltage of 0-3.3V








