Dial Gauge With Pi Pico, SSD1306 and MicroPython - Advanced Graphics Routines

by tonygo2 in Circuits > Microcontrollers

1382 Views, 3 Favorites, 0 Comments

Dial Gauge With Pi Pico, SSD1306 and MicroPython - Advanced Graphics Routines

Canon-20.jpg

In this project I will show you how to display a Dial Gauge on an SSD1306 128x64 mono pixel display. It is driven by a Raspberry Pi Pico using MicroPython. The value displayed is controlled a potentiometer. All the components are inexpensive and widely available.

Supplies

ssd1306.jpg

We will need the following Items:

  • Raspberry Pi Pico with soldered pins
  • Breadboard
  • SSD1306 128x64 pixel display
  • 10K Ohm potentiometer
  • Connecting wires
  • Button switch
  • LED and 330 Ohm current limiting resistor (Optional)
  • Thonny Editor installed on your computer
  • USB cable between Pico and computer

Build the Circuit

ssd1306.jpg

SSD1306 has SDA on GP8, SCL in GP9, VCC on 3.3 volt rail and GND on GND rail

The button is on GP15 and 3.3 volts, with pull-down

The potentiometer is connected ADC0 on GP26

The LED, if fitted, is on GP16 (Optional)

Try It Out

Pi Pico and SSD1306 Advanced Graphics - Dial Gauge - MicroPython

Run the video to see it working.

Download the SSD1306.py library and install it on your Pico.

Download the program and execute it from Thonny.

Setup Section

dial1-50.jpg

This section imports the required library routines and sets up the hardware - all very normal.

The listing includes the top of the character definitions for the re-sizeable font. It is quite long but the Pico has plenty of memory

Font Control

Dial 291 - 330.jpg

This shows the end of the extended font definitions and the three procedures to display text on the display.

  1. character(asc,xt,yt,sz,c) = Single character at (xt, yt) in size, sz and colour c
  2. prnt_st(asci,xx,yy,sz,c) = prints string of characters from (xx, yy) in size sz and colour c
  3. cntr_st(s,y,sz,c) = Centres a string on line y in size sz and colour c

Extra Routines

Dial 332-375.jpg

The SSD1306 library inherits pixel(), vline(), hline(), line(), fill(), show() and rect() from framebuf. You can find the documentation here:

https://docs.micropython.org/en/latest/library/framebuf.html?highlight=framebuf#module-framebuf

(Not all have been implemented at the moment.)

I have added the following routines:

block(x,y,w,h,c): # Filled rectangle

circle(x,y,r,c): # Filled circle

ring(x,y,r,c): # Edge of circle

get_ADC(max_required) = Read ADC and rescale value from 0 to max_required

Build the Dial

Dial 375-410.jpg

Here we display the 'Splash screen' or title using centred text in 2 sizes.

We start drawing the dial with the outer ring and then draw radial lines from the centre to the ring using a bit of trigonometry. The ticks are far too long; so we shorten them by drawing a black filled circle over them. We then draw a black rectangle to remove the arc at the bottom of the dial between 0 and 100 ticks.

Finally I put my name n the display. (Change to whatever you want.)

Note that we only draw the dial ring once. We do not wipe the whole screen and rebuild from scratch each time we loop.

Move the Needle Round the Dial

Dial 411-433.jpg

Here well run a loop counting the percentage from 0 to 100 and make the needle move. Look carefully to see that we rub out the previous needle and displayed value before drawing in the new versions.

The text is right aligned before being displayed. This stops it moving from side to side as its length increases.

Move the Pot to Control the Needle

Dial 433 - end.jpg

This loop continues until the button is pressed. It gets a value for the percentage to to be displayed from the potentiometer. Twist the knob to change the value.

Once the button is pressed the looping halts and we say BYE with size 3 characters and tidy-up the display.



Conclusions

Canon-21.jpg

The Trigonometry and Pythagoras we did at school has at last come in for something useful!

If you have found this useful, please leave a comment.

You could also suggest a topic for another Pi Pico + MicroPython project you would like me to cover.

Have fun with your coding.