Build this circuit on breadboard or use a simulator software. We're seeing 5volts in red and the 0volts in blue. On a real circuit we would use a scope or voltmeter.
See how each lead of the display component has a letter a to g with a dot and -ve power connection. 5volts on the a pin turns on the top bar of the display? These are often labelled on the physical device.
Power Each SegmentGo down the switches and turn on one segment at a time. The 8th lead goes to the dot and often is left out of code and connections. Your code can use the dot without it connected, it just won't show.
Combine the segments to make visible numbers. Our demo code will add some combinations for letters.
This is not something we want to do manually. It is exactly the thing we want to automate with a program. The ATmega328 is a single chip computer, connect power and it will run any program in flash memory.
The ATmega328 is the processor in an Arduino Uno board. We can do our programming with Arduino IDE in a language called C++ or C-plus-plus. A demo program will use only 2kB of our 32kB of flash on the board.
First we have to install a library program that will add code that knows how to work with the LED circuit.
Advanced Seven SegmentUpdate your library of Arduino code and search for Advance Seven Segment. There are a lot of software libraries written for these displays. This one is quite small.
It adds two files of source code that you can open and read, modify. The header file is AdvanceSevenSegment.h and it provides a declaration for a function we will use. We include this header file in our code and functions from this library can be used in our sketch.
And AdvanceSevenSegment.cpp which provides function definitions. Notice the 1's and 0's match up with 5volts and 0v on the output pins. Reading through source code shows you how the program works. It can be readable.
A lot of principles of C++ and OOP object oriented programming are demonstrated here. The ones and zeros going in become data objects and so do our LED segments.
Program Your Uno BoardConnect the wires like you see in the diagram and upload the sketch. Notice that when pin13 is energized the LED on the Uno board lights, too.
Open an Arduino sketch and copy in this code. It needs the included library. The display module cycles through the digits 0 to 9 then some alphanumerics that work on 7 segment displays.
#include <Arduino.h>
#include <AdvancedSevenSegment.h>
// included library function uses IO A0 = 14, A1 = 15
AdvanceSevenSegment display(8, 9, 10, 11, 12, 13, 14, 15);
void setup() { }
void loop() {
for (int i = 0; i <= 9; ++i) {
display.setNumber(i);
delay(1000);
}
display.refresh();
char list[] = {'a','b','c','d','e','f','h','l','p','q','u','y'};
for (char c : list) {
display.setCharacter(c);
delay(1000);
}
display.refresh();
}
A key line is the data object constructor AdvanceSevenSegment display(variables); mathematical function that takes the ones and zeros for each display segment and calculates whether to turn each on.
Include FilesWe looked at the library header and cpp code, earlier. Go back and look in the header and cpp source files to see how the program turns alphanumeric values into voltages.
Notepad opens these files.
We write #include<Arduino.h> in our sketch and then all of the above are added, too. <avr/pgmspace.h> is located in another directory "binary.h" is located in the same one.
So, that's it then?No, just getting started. Here we see only the basic 7 segment LED component. Many more circuit combinations involve multiple displays, additional interfaces and chips.
Our single module uses 7 or 8 pins on our board. Adding modules doesn't work this way. To deal with all the connection leads there are solutions to work serially on 2 wires, or reduce connections to four lead BCD binary coded decimal.
Our module uses ground for all of our segments. We apply 5v or 3v to each bar to get light. This is called shared or common cathode type. Other common anode modules are wired to use a shared positive terminal. We may need to flip our bits.
Be creative. Imagine the Uno running this sketch as a display driver device. Our sketch is display driver software. You can use the serial port feature to print your choice of message.
This sketch is small. Run it on a smaller chip like the ATmega8 or ATtiny. Smaller physically, we only use 8 pins.
Finding code and circuit diagrams for 7 segment can be messy because there are so many options. This project is not a shift-register project, it is not BCD and it is not a serial interface. Each of these signal interfaces are distinct.
This project is about the module with 7 segments a, b, c, d, e, f, g. Sometimes called tubes because hot glowing vacuum tubes were a display technology.
How would you write a library to make an Arduino morse code keyer? How to you get the serial monitor working so that you can type letters on your keyboard and get morse code on the LED?
Comments
Please log in or sign up to comment.