Sample Pixel-Based Drawing Program
Welcome to Etch-A-Sketch
In this lab, you will build a digital Etch-A-Sketch! Two potentiometer knobs control where a line is drawn on the OLED screen. Let's build something amazing!
Code example provided by Jim Tannenbaum.
This program works like an Etch-A-Sketch toy. You use two potentiometers to control drawing. One knob controls the X (left-right) position. The other knob controls the Y (up-down) position. As you turn the knobs, the program draws a continuous line on the OLED display.
Press the reset button to clear the screen and start over.
How It Works
The program reads the two potentiometer values every 0.2 seconds. It scales each raw reading (0 to 65,535) to the matching screen coordinate. Then it draws a line from the previous position to the new position.
This is the same idea behind how classic drawing tablets work.
Key Idea
The scaled() function converts one range of numbers to another. For example, a knob value of 32,768 (halfway between 0 and 65,535) maps to pixel 64 (halfway across a 128-pixel screen).
Wiring Steps
- Connect the OLED GND to Pico GND.
- Connect the OLED VCC to Pico 3.3V.
- Connect the OLED SDA to Pico GP4 (or match your SPI wiring).
- Connect the OLED SCL to Pico GP5 (or match your SPI wiring).
- Connect the center tap of the vertical potentiometer to Pico GP26 (ADC0).
- Connect the center tap of the horizontal potentiometer to Pico GP27 (ADC1).
- Connect the outer legs of each potentiometer to 3.3V and GND.
- Connect one leg of the reset button to Pico GP14 and the other to GND.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
What each section does:
scaled(value, istart, istop, ostart, ostop)— converts the knob value (0–65535) to a screen coordinate. For x it converts to 0–127; for y it converts to 0–63.SPI(0, baudrate=100000, ...)— creates a fast SPI connection to the display at 100,000 bits per second.ssd1306.SSD1306_SPI(128, 64, spi, DC, RES, CS)— creates the display object using SPI.while reset_button.value() != 1— waits for the user to press the button before starting.ADC(26)andADC(27)— read the two potentiometer knob values.start_x = new_x = scaled(...)— sets both the start and end of the first line to the same point.oled.line(start_x, start_y, new_x, new_y, 1)— draws a line from the last position to the new position. This creates a smooth continuous line as you turn the knobs.start_x = new_x— saves the new position so next time the line starts from here.if reset_button.value(): oled.fill(0)— checks the reset button every loop. If pressed, clears the screen.
Monty's Tip
Try changing time.sleep(0.2) to time.sleep(0.05) for a faster, smoother drawing experience. Smaller delay = more responsive knobs!
You Can Do This!
If the line looks jumpy, check that both knobs are wired correctly. Connecting outer legs to 3.3V and GND is easy to mix up — that is completely normal. Swap the outer wires if the knob seems backwards.
Great Work!
You built a working Etch-A-Sketch with MicroPython! Next, you will draw random hearts that appear all over the screen.