SunFounder ESP32 Starter Kit
from machine import ADC,Pin
import time
xAxis = ADC(Pin(34, Pin.IN)) # create an ADC object acting on a pin
xAxis.atten(xAxis.ATTN_11DB)
yAxis = ADC(Pin(35, Pin.IN)) # create an ADC object acting on a pin
yAxis.atten(yAxis.ATTN_11DB)
button = Pin(33, Pin.IN, Pin.PULL_UP)
while True:
xValue = xAxis.read() # read a raw analog value in the range 0-4095
yValue = yAxis.read() # read a raw analog value in the range 0-4095
btnValue = button.value()
print(f"X:{xValue}, Y:{ yValue}, Button:{btnValue}")
time.sleep(0.1)
When the program runs, the Shell prints out the x, y, and button values of joystick.
X:1921, Y:1775, Button:0
X:1921, Y:1775, Button:0
X:1923, Y:1775, Button:0
X:1924, Y:1776, Button:0
X:1926, Y:1777, Button:0
X:1925, Y:1776, Button:0
X:1924, Y:1776, Button:0
• The x-axis and y-axis values are analog values that vary from 0 to 4095.
• The button is a digital value with a status of 1(release) or 0(press).
3.30 5.12 Measuring Distance
The ultrasonic module is used for distance measurement or object detection. In this project, we will program the module
to obtain obstacle distances. By sending ultrasonic pulses and measuring the time it takes for them to bounce back, we
can calculate distances. This enables us to implement distance-based actions or obstacle avoidance behaviors.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
3.30. 5.12 Measuring Distance 395