Simulated 7-Segment Numbers With Pico and Waveshare Display

by tonygo2 in Circuits > Microcontrollers

944 Views, 3 Favorites, 0 Comments

Simulated 7-Segment Numbers With Pico and Waveshare Display

7seg pix-2.jpg

The 1.8" LCD Display for Raspberry Pi Pico is a 65K colours, SPI driven, 160x128 pixel screen for the Raspberry Pi Pico. It is inexpensive (£9 in UK), fast, bright and uses the minimum of pins to connect via SPI leaving the user with plenty of pins for other project components such as buttons, sensors or potentiometers. The updated driver supports a single size of text characters but does include lower-case letters. It would be useful if we could show larger numeric values on the screen without using up too much memory.

7-segment LED displays are easy to read and support numeric values, decimal and hexadecimal. They should be easy to simulate on the screen in various sizes and colours and will not need to be continuously refreshed like real 7-segment displays. I'm going to use MicroPython for this Instructable.

Supplies

Raspberry Pi Pico

Waveshare 1.8" LCD Display for Raspberry Pi Pico - (£9 from ThePiHut.com and others in UK)

USB cable

Thonny editor

The Finished Project

7seg pix Title-1.jpg

The video shows the system working with different sizes of 7-segment digits and letters needed for hexadecimal values. If you want to try it on your own system just download the code here.

The following pages provide more explanation of how it all works.

Coding the Characters

CodingSystem.jpg

Each symbol is made up from the 7 segments shown in red on the diagram. Each segment has a letter. The symbol for the number 8 needs all 7 segments so is coded with all 1s. The zero symbol does not need the centre segment so that segment, G, gets coded with a 0. The symbol 1 needs only 2 segments lit up so gets two 1s in the B and C positions. This is a very easy code.

Code - Part 1

1-43.jpg

Waveshare have provided their display driver as normal MicroPython code, which is just pasted into your program rather than being called from a separate library which has been loaded separately. As long as you are careful with the copy and paste you do not need to look at the driver code, which is almost impossible to follow.

Lines 4 to 13 import the necessary library routines and set up the pins to drive the display. The rest of the picture shows the beginning of the driver code.

Code - Part 2

156-191.jpg

This page shows the last few lines of the driver code, a routine to set the colour in 2 bytes, needed by the display, from the more usual colour definition in 3 bytes(R,G,B) with values in the range 0 - 255. (All this has been explained in one of my previous Instructables.)

The final section sets up the display, its brightness and clears the screen to a black background.

Code - Part 3

190-223.jpg

This page is the meat of the project. The codes for the characters are saved and the routine to draw a single character in various sizes is shown.

seg(xx, yy, n, f, bg, fg)

This draws one symbol with the origin(top-left) at (xx, yy), symbol number n with size factor f with a background colour of bg and a foreground colour of fg.

Because the pixel(x, y, colour) instruction includes the colour information we only have to go through the code once. We just change the colour of a segment to update the character shown. This is very efficient.

Code - Part 4

223-245 Title.jpg

There code here draws the title screen with a blue box and white corner dots showing that this is the newer driver with origin at (0,0), the full 160 pixels across the screen and full 128 pixels down the screen. It shows a strange looking 7-Seg in the centre with segmented characters of different sizes and the title messages.

Code - Part 5

246-278 last.jpg
7seg pix-3.jpg

This is the bit that shows the results. It counts up in blocks of 20 numbers showing the various sizes possible from 1 - 9. It then goes through the characters one at a time - including the minus, lower case 'g' and 'blank'.

I hope you have found this Instructable useful and that it encourages you to try it out or build something similar with your own devices.

Feedback, questions, likes, links and hearts are welcome.

Enjoy your coding.