Wire the Circuit
Write the Program
Read more- Use these steps to wire up the LED and sonar circuit before writing the program.
- The following first two circuits show the nodes for meeting points on a breadboard and the Raspberry PI Pico W wiring diagram
- Put the 220 ohm resistor on the breadboard with each leg in a different row.
- Put the LED in the breadboard with the short leg on the same row as the 220 ohm resistor
- Put the long leg of the LED on a different row and connect the long leg of the LED to pin 28
- Use a wire to connect the other leg of the 220 ohm resistor to ground.
- Use the sonar mount to connect the sonar to the front of Lily∞Bot.
- Use a wire to connect the VCC pin on the sonar to the 3.3 V (OUT) pin on the Raspberry PI Pico W. This pin is 5th from the top on the right
- Use a wire to connect the GND pin on the sonar to any ground pin on the Raspberry PI Pico W.
- Use a wire to connect the TRIG pin on the sonar to pin 3 on the Raspberry PI Pico W.
- Use a wire to connect the ECHO pin on the sonar to pin 2 on the Raspberry PI Pico W.
Breadboard Nodes
Raspberry PI Pico W PInOut
1 / 2 • Sonar and LED circuit
- Create a.py file in Microsoft Visual Studio in Platform IO
- Use CTRL-SHIFT-P or CMD-SHIFT-P to configure the code as a MicroPic project.
- Paste the following code in the.py file
- Save the file and right click on name and select "Run current file on Pico"
#CAB 9.14.23 sonar.py
#Use sonar to control LED brighness on Lily∞Bot
#LED on pin 28, potentiomater on pin 27
#https://www.noiresteminist.com/shop
#VCC to pin 40
#Echo to pin 2
#Trigger to pin 3
#ground to GND
#LED to pin 28
from machine import Pin, ADC, PWM
from utime import ticks_us, sleep_us, sleep_ms
#define inputs and outputs
ledPin = 28
triggerPin = 3
echoPin = 2
#led = Pin(ledPin, Pin.OUT)
led = PWM(Pin(ledPin))
led.freq(60)
trigger = Pin(triggerPin, Pin.OUT)
echo = Pin(echoPin, Pin.IN)
print("Sonar on Lily∞...")
# Will return an integer between out_min and out_max
def convert(x, i_m, i_M, o_m, o_M):
return max(min(o_M, (x - i_m) * (o_M - o_m) // (i_M - i_m) + o_m), o_m)
################# function definitions
def distance():
timepassed=0
signalon = 0
signaloff = 0
trigger.low()
sleep_us(2)
trigger.high()
sleep_us(5)
trigger.low()
while echo.value() == 0:
signaloff = ticks_us()
while echo.value() == 1:
signalon = ticks_us()
#print(signalon)
#print(signaloff)
timepassed = signalon - signaloff
#print(timepassed)
dist_cm = (timepassed*0.0343)/2
if dist_cm>60:
dist_cm=60
return dist_cm
################## Main ########################
while True:
reading = distance()
#print(reading)
scale = convert(reading, 10, 60, 0, 60000)
print(scale)
led.duty_u16(int(scale))
sleep_ms(100)
############### Mapping Functions ####################################
# Will return a float
#def convert(x, in_min, in_max, out_min, out_max):
# return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
# Will return a integer
#def convert(x, in_min, in_max, out_min, out_max):
# return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min
# Test
#for i in range(200):
# print(i, convert(i, 40, 80, 0, 1023))
Verify that it's working- If the circuit is working correctly it should look like the following video.
- If it is not working, try to debug your code and troubleshoot the circuit wiring.
14 projects • 22 followers
Carlotta Berry is a Professor and Dr. Lawrence J. Giacoletto Endowed Chair for Electrical and Computer Engineering at Rose-Hulman.
Comments
Please log in or sign up to comment.