Why is ADC important?
In the digital world of computers and microcontrollers, it's essential to convert real-world, analog signals into digital data that can be processed, analyzed, or stored.
from machine import ADC, Pin
adc = ADC(Pin(34)) # Create ADC object on ADC pin
adc.atten(ADC.ATTN_11DB) # Set attenuation for greater range
reading = adc.read() # Read a value in range 0-4095
print(reading)
The ATTN
parameter in the adc.atten()
method sets the attenuation level for the ADC, which essentially determines the maximum voltage range that the ADC can measure.
ADC.ATTN_0DB
: Range 0 - 1.1VADC.ATTN_2_5DB
: Range 0 - 1.5VADC.ATTN_6DB
: Range 0 - 2.2VADC.ATTN_11DB
: Range 0 - 3.3V (full range)For most applications with the ESP32, you'll use ADC.ATTN_11DB
to get the full range of readings.
A potentiometer is a variable resistor. By turning its knob, you can vary the resistance, which in turn varies the voltage across it. This changing voltage can be read by the ADC on the ESP32.
from machine import ADC, Pin, I2C
# ADC setup
adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)
reading = adc.read()
adc_str = f'Pot Value: {reading}'
print(adc_str)
from machine import ADC, Pin, I2C
from utime import sleep
# ADC setup
adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)
while True:
reading = adc.read()
adc_str = f'Pot Value: {reading}'
print(adc_str)
sleep(0.1)
Photocells, also known as LDRs (Light Dependent Resistors), change resistance based on the amount of light they receive. The brighter the light, the lower the resistance.
// These constants should match the photoresistor's "gamma" and "rl10" attributes
const float GAMMA = 0.7;
const float RL10 = 50;
// Convert the analog value into lux value:
int analogValue = analogRead(A0);
float voltage = analogValue / 1024. * 5;
float resistance = 2000 * voltage / (1 - voltage / 5);
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
# Constants for the photoresistor's attributes
GAMMA = 0.7
RL10 = 50
def calculate_lux(analogValue):
# ESP32 ADC is 12-bit, so max value is 4095 and voltage range is 5V
voltage = analogValue / 4095 * 5
resistance = 2000 * voltage / (1 - voltage / 5)
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA))
return int(lux)
from machine import ADC, Pin, I2C
from utime import sleep
import ssd1306
from math import pow
# Constants for the photoresistor's attributes
GAMMA = 0.7
RL10 = 50
def calculate_lux(analogValue):
voltage = analogValue / 4095 * 5
resistance = 2000.0 * voltage / (1 - voltage / 5)
lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA))
return int(lux)
# ADC setup
adc = ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)
# OLED setup
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
while True:
reading = adc.read()
lux = calculate_lux(reading)
# Clear display
oled.fill(0)
adc_str = f'Pot Value: {lux:5}'
oled.text(adc_str, 0, 0)
oled.show()
sleep(0.1)
from machine import ADC, Pin, I2C
from utime import sleep
import ssd1306
from math import pow
# ADC setup
adc = ADC(Pin(39))
adc.atten(ADC.ATTN_11DB)
# ESP32 Pin assignment
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
oled_width = 128
oled_height = 64
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c)
while True:
reading = adc.read()
# Clear display
oled.fill(0)
adc_str = f'Pot Value: {reading}'
oled.text(adc_str, 0, 0)
oled.show()
print(adc_str)
sleep(0.1)
Pin Name | Description |
---|---|
VCC | Positive power supply |
VERT | Vertical axis output (analog) |
HORZ | Horizontal axis output (analog) |
SEL | Push button |
GND | Ground |
from machine import ADC, Pin
# Initialize analog pins for horizontal and vertical axes
vert = ADC(Pin(4))
horz = ADC(Pin(2))
vert.atten(ADC.ATTN_11DB)
horz.atten(ADC.ATTN_11DB)
# Initialize SEL pin for the push button
button = Pin(15, Pin.IN, Pin.PULL_UP)
def read_joystick():
vertical_val = vert.read()
horizontal_val = horz.read()
button_status = not button.value() # Button pressed when value is 0
return vertical_val, horizontal_val, button_status
while True:
v, h, b = read_joystick()
print("Vertical:", v, "Horizontal:", h, "Button Pressed:", b)
time.sleep(0.5)
Add ESP32 pin numbers and adjust timing as needed.
ADC is used as a PWM width value to control devices such as buzzers, LED brightness, and servo motor rotation angles.