Go Down

Topic: Code for driving a 7-segment display (Read 5693 times) previous topic - next topic


I just got my first arduino board, the Nano, and for one of my first projects I wanted to make a digital thermometer. I have some C/C++ knowledge, but it is limited. I am using an LM34 temp sensor and I want to drive a 2-digit 7-segment LED display (Common Cathode).

The part number is obscure, and I cannot find a data sheet. Assume I can figure out which pin goes to what LED, which isn't a difficult task. That is not the problem.

I will be driving it with two 74LS48, which is a BCD to 7-segment display driver. This is because there are only 12 digital I/O pins (leaving the possibility for 3 displays)

Say the temp sensor reads 0.7V for 70F, and I use the 1.1V internal analogReference. The arduino would ideally detect (0.7V / 1.1V)*1023 = (651)10 or (1010001011)2 on its ADC output.

I would want to display 70 on the display, which would be BCD number 0111 0000 (This is the data which would be sent in parallel to the two 74LS48s)

Say the program does the following and converts the analogRead to a familiar decimal (integer) temperature reading:

Code: [Select]
temp = analogRead(LM34_pin);           // stores the digitized (0 - 1023) analog reading from the LM34
fahrenheit = (100*temp*(vref))/1023;   // calculates the actual fahrenheit temperature

How would I convert the decimal 70 to a form that will drive the 8 pins driving the display drivers (drive!)? (I can skip the conversion altogether if need be)

I have been searching the forums and internet for code to steal, but I have not had any luck. I would appreciate any help.

Thanks in advance.


How would I convert the decimal 70 to a form that will drive the 8 pins driving the display drivers

Assume the two numbers you want to output are msb and lsb (most significant bit & least significant bit)

display = 70;
msb = display / 10;
lsb = display - (msb * 10);

would give msb = 7 and lsb = 0
Is that what you mean?


That is definitely helpful, thank you. Don't know why I didn't think of that part on my own, but then I need to convert that to BCD and send each digit to a separate digital output. I have thought of ways to do this, but I'd like the most efficient way someone can come up with, and my way isn't efficient.



but then I need to convert that to BCD and send each digit to a separate digital output

But that is BCD code. Each digit is in a separate variable. Each digit is a binary number. Just send each digit to the pins you have wired up your seven segment decoder up to.


display = 70;
msb = display / 10;
lsb = display % 10;

would give msb = 7 and lsb = 0 also.


Say I had decimal 7 in the MSB variable, I would need to take that and turn it into it's BCD equivalent, 0111, and send it to 4 digital pins separately. Using a total of 8 pins to drive two BCD to 7-segment display drivers.

Again, for the number 70, I would want the following outputs:
(Pins chosen arbitrarily)

MSB = 7

2 = (MSB 1) = LOW
3 = HIGH
4 = HIGH
5 = (LSB 1) = HIGH

LSB = 0

6 = (MSB 2) = LOW
7 = LOW
8 = LOW
9 = (LSB 2) = LOW


Feb 02, 2009, 03:26 pm Last Edit: Feb 02, 2009, 04:40 pm by Grumpy_Mike Reason: 1
Try something like this:-

Code: [Select]

int msbPins[] = {2,3,4,5};  // pins to use for msb
int lsbPins[] = {6,7,8,9};
int val;

msb = 7;
lsb = 0;

for(int i=0; i<4; i++){
val = msb & 1; //  isolate last bit of msb
digitalWrite(msbPin[i],val); // send to correct pin
val = lsb & 1; //  isolate last bit of lsb
msb = msb >> 1; // shift the bits down so the next one is in position zero
lsb = lsb >> 1;

Use an array to define the order of your pins wired up to the driver.
Output high or low for the least significant bit of your number (msb and lsb)
then shift msb & lsb one place to the right so the next least significant bit becomes the least significant bit, loop round and do it again.

Note this is just written not compiled, it is not a complete programme.


Thanks, I'll take a look at that when I get off work.

Go Up