Pi Pico emulating an Etch-a-sketch
One Christmas, long, long ago, someone gave me an Etch-a-sketch.
I was a bit of a klutz then (indeed, I still am). I never produced great works of art, but the Etch-a-sketch was a lot of fun to play with.
Yesterday I wondered how easy it would be to emulate an Etch-a-sketch using the Raspberry Pi Pico.
Using PySerial to link Pi and Pico
I’d just finished yesterday’s article about how to link a host to the Pico using PySerial.
Why not
- get the Pico to print out how far two potentiometer knobs were turned,
- read the output on the Pi using PySerial,
- and use the Turtle package on the Pi to create the display?
It sounded too simple to work, so I started coding with some trepidation.
The first job was to connect a couple of potentiometers to the analogue inputs of the Pico.After that I wrote a short MicroPython program which read the analogue values and printed them out.
Here it is:
import machine
import utime
pot_l = machine.ADC(27)
pot_r = machine.ADC(26)
def run():
while True:
print(pot_l.read_u16(), pot_r.read_u16())
utime.sleep_ms(100)
run()
I installed the program on the Pico and checked that the output changed when I rotated the knobs.
Next I wrote another short Python program on the Raspberry Pi. It uses sender.py
, described in yesterday’s article.
Here’s the code:
from sender import Sender
import turtle
s = Sender()
scr = turtle.getscreen()
t = turtle.Turtle()
def convert(text):
return (int(text)-200)//300
while True:
text = s.receive()
x,y = [convert(text) for text in text.split()]
t.setx(x)
t.sety(y)
It worked!
My next task is to adapt the code to create a very simple audio frequency oscilloscope. I’ll write that up next week.
To keep up to date with this series follow @rareblog on twitter.
Comments
Post a Comment