Saturday, 30 January 2021

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.

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())


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, 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()]

It worked!

I used a Pimoroni Explorer base for the version in the photo at the top, but you can use a Pico with a standard breadboard. Here's the Fritzing diagram.

Here’s some output, confirming that my coding is better than it was when I was young, but my drawing ability isn’t :)

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.

No comments:

Post a comment