Hardware preparation
- Potentiometer x 1
- TB6612FNG module x 1
- 5V DC motor x 1
- Some wires for connection
BPI-Leaf-S3 :
The ESP32-S3 chip on the BPI-Leaf-S3 has two ADC analog to digital converter integrated inside the chip, measuring from 0mV to 3100mV.
Two ADC modules each have 10 measuring channels, ADC1 measuring GPIO1 ~ 10, ADC2 measuring GPIO11 ~ 20.
Use ADC to read the voltage value of the potentiometer
A potentiometer is a three-terminal resistor with a sliding or rotating contact that forms an adjustable voltage divider. If only two terminals are used, one end and the wiper, it acts as a variable resistor or rheostat.
How to connect
Potentiometer | BPI-Leaf-S3 |
---|---|
GND | GND |
VCC | 3V3 |
S | 14 |
MicroPython Code
from machine import Pin,ADC
import time
adc14 = ADC(Pin(14),atten=ADC.ATTN_11DB)
#adc14 = ADC(Pin(14))
#adc14.atten(ADC.ATTN_11DB)
while True:
read=adc14.read()
read_u16=adc14.read_u16()
read_uv=adc14.read_uv()
print("read={0},read_u16={1},read_uv={2}".format(read,read_u16,read_uv))
time.sleep_ms(100)
When the hardware connection is correct, after the program starts to run, it will continue to get three data at intervals of 100ms
-
ADC.read()
function reads ADC and returns its values, ESP32-S3’s ADC returns 12bit precision data. -
ADC.read_u16()
function reads ADC then returns 16bit data. -
ADC.read_uv()
function takes an analog reading and return an integer value with units of microvoltsuV
. The return value only has’mV’ resolution(meaning it will always be the multiplier of 1000 microvolts.
Use ADC measuring potentiometer to control the motor
How to connect
Potentiometer | BPI-Leaf-S3 |
---|---|
GND | GND |
VCC | 3V3 |
S | 14 |
TB6612FNG | BPI-Leaf-S3 |
---|---|
PWMA | 11 |
AIN2 | 13 |
AIN1 | 12 |
STBY | 10 |
VM | 5V |
VCC | 3.3V |
GND | GND |
TB6612FNG | Motor |
---|---|
AO1 | Motor N pole |
AO2 | Motor S pole |
MicroPython Code
from machine import Pin,ADC,PWM
import time
adc14 = ADC(Pin(14),atten=ADC.ATTN_11DB)
PWM_A = PWM(Pin(11)) #Set PWM output pin
PWM_A.freq(20000) #Set PWM frequency
PWM_A.duty(0) #Set PWM duty cycle
AIN1 = Pin(12,Pin.OUT)
AIN2 = Pin(13,Pin.OUT)
STBY = Pin(10,Pin.OUT)
AIN1.on() #MOTOR forward
AIN2.off()
STBY.on() #When STBY pin is at high level, TB6612FNG starts.
while True:
read_mv=adc14.read_uv()//1000
if read_mv <= 3000:
duty_set = int(1023/3000 * read_mv)
else:
duty_set = 1023
PWM_A.duty(duty_set)
Duty_cycle = int(duty_set/1023*100)
print("ADC_read={0}mv,Duty_cycle={1}%".format(read_mv,Duty_cycle))
time.sleep_ms(100)
wiki page: https://wiki.banana-pi.org/BPI-Leaf-S3