Display of float data type on 7 segment led display

Hi,

I used LCD to display the float data types that includes decimals for all analog sensors, but for a specific application i wanted to display the sensor data on a 4 digit 7 segment led display.

My experimentation began with using 2 shift registers (74HC595) using one of them to drive the segments of the Led Display & other switching the digits of the 7 segment display. I wrote the code & it works for 'int' data type where there is NO decimal involved. Here is the code

/*Program to display data on a 2 digit Common Anode 7 segment LED Display using 2 74HC595 Shift Registors.

Written By
Shiharan Choudhury
utomate With Arduino
*/

int clockPin = 8; // Pin 8 of Arduino connected in the pin 11 of 74HC595 (Clock)
int latchPin = 9; // Pin 9 of Arduino connected in the pin 12 of 74HC595 (Latch)
int dataPin = 10; // Pin 10 of Arduino connected in the pin 14 of 74HC595 (Data)
int SensorPin = A0; // Pin A0 of Arduino connected to the data pin of analog sensor

int UnitData; //Data to be displayed on the unit place
int TenData; //Data to be displayed on the ten'th place
int Parameter; // The parameter to measure using sensor
int SensorDataValue; // Variable to store sensor data value

//Digits Matrix - 0 a 9
byte numbers[] = {
B01111110, // Zero
B00110000, // One
B01101101, // Two
B01111001, // Three
B00110011, // Four
B01011011, // Five
B01011111, // Six
B01110000, // Seven
B01111111, // Eight
B01111011, // Nine
};

void setup() {

pinMode(latchPin, OUTPUT); // Declaring the PIN 8 of the arduino as Output
pinMode(clockPin, OUTPUT); // Declaring the PIN 9 of the arduino as Output
pinMode(dataPin, OUTPUT); // Declaring the PIN 10 of the arduino as Output
pinMode(SensorPin, INPUT); // Declaring the PIN 2 of the arduino as input
Serial.begin(9600); // begin serial communication at 9600baud rate
}
void loop() {

// This is where we will make all the calculations of the sensor data including ADC

Parameter = analogRead(A0); // Reading the sensor data
SensorDataValue = Parameter*0.00488758; // Converting sensor data into digital equivalent by multiplying 5/1023
UnitData = SensorDataValue % 10;
TenData = SensorDataValue / 10;

// Here we set the approperiate display & coresponding data to send from the arduino to the 7 segment LED display.

digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 8 ); //Set Digit 1 (top view from left to right)
shiftOut(dataPin, clockPin, LSBFIRST, ~numbers[TenData]); //Set the SensorDataValue (TenData)
digitalWrite(latchPin, HIGH);

digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, 4); //Set Digit 2
shiftOut(dataPin, clockPin, LSBFIRST, ~numbers[UnitData]); //Set the SensorDataValue (UnitData)
digitalWrite(latchPin, HIGH);

Serial.println(SensorDataValue); // print the light on serial monitor
delay(750);
}

Here the sensor data is displayed as a number ranging from 0-99 but i would like to display accurate data with decimal places using float data type on the 7 segment LED display. I would appreciate if someone educates me about the process where i can display decimal values on the display such as 0.18/1.95/2.87/99.91 etc etc

Two different possibilities are:

  1. Use the function dtostrf() to convert the value to a character array, and output that array to the display.

  2. Don't use floats. For example, use an integer to represent temperature in degrees C*10 to one digit fractional precision. Simply put a decimal point in front of the last digit.

1 Like

Hi,

First of all I think it's best to make your display display a 4 digit int, and convert the floating point to a integer first. In your case this can be done by adding the line

float sensorData = 34.23;
int floatInInt = (int)(sensorData*100);//The variable that contains the floating data, but in an int form so your display can display it.

Then you should light up the middle point of your 4-digit display and voila! This simple approach only works if you want to display a fixed number of digits behind the point. Otherwise you should say something like

int floatInInt;
if(sensorData <= 1){
floatInInt = (int)(sensorData*1000);
//Light up the first point
}

I hope you can do something with this information!

Jasper

1 Like

Thanks jremington & Jasper for your valuable time in posting replies. However over night i have written a different code (Without Shift Registers) to display analog sensor data in a 3 digit 7 segment led display with 1 decimal place. Here is the code

// Program to display analog sensor data with 1 decimal place on 3 digit 7 segment LED Display.

int a; // Variable to store decimal data
int Din; // Variable to store computed sensor data
int input = A0; // Sensor DataPin connected to PIN A0 of arduino
int disp1=1; // 1st Digit of the display, (Left to Right) connected to PIN 1 of the Arduino
int disp2=2; // 2nd Digit of the display
int disp3=3; // 3rd Digit of the display
int segA=5; // Segment A of the LED display
int segB=6; // Segment B of the LED display
int segC=7; // Segment C of the LED display
int segD=8; // Segment D of the LED display
int segE=9; // Segment E of the LED display
int segF=10; // Segment F of the LED display
int segG=11; // Segment G of the LED display
int segDP=12; // Segment DP of the LED display
void setup()
{
pinMode(disp1, OUTPUT);
pinMode(disp2, OUTPUT);
pinMode(disp3, OUTPUT);
pinMode(segA, OUTPUT);
pinMode(segB, OUTPUT);
pinMode(segC, OUTPUT);
pinMode(segD, OUTPUT);
pinMode(segE, OUTPUT);
pinMode(segF, OUTPUT);
pinMode(segG, OUTPUT);
pinMode(segDP, OUTPUT);
pinMode(input,INPUT);
}
void loop()
{
Din=analogRead(input);
Din=Din*5/1023; // ADC
a = Din%10; // Taking mod of Din

if(a<=5) // Comparing the value of a to be within 0-5
{a=0;} // Displaying a=0 when value of a lies between 0-5
else
{a=5;} // Displaying a=5 when value of a lies between 6-10

//Section to switch approperiate digit of the display based on computed data
digitalWrite(disp1,LOW);
digitalWrite(disp2,LOW);
digitalWrite(disp3, HIGH);
digitalWrite(segDP,HIGH);
display(a);
delay(5);
Din = Din/10;
a = Din%10;
digitalWrite(disp3,LOW);
digitalWrite(disp2,HIGH);
digitalWrite(segDP,HIGH);
display(a);
delay(5);
Din=Din/10;
a=Din;
digitalWrite(disp2,LOW);
digitalWrite(disp1,HIGH);
digitalWrite(segDP,LOW);
display(a);
delay(5);

}
int display (int a)
{
switch (a)
{
case 0:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, HIGH);
break;

case 1:
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;

case 2:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, HIGH);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, HIGH);
digitalWrite(segG, LOW);
break;

case 3:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, LOW);
break;

case 4:
digitalWrite(segA, HIGH);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 5:
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 6:
digitalWrite(segA, LOW);
digitalWrite(segB, HIGH);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 7:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, HIGH);
digitalWrite(segE, HIGH);
digitalWrite(segF, HIGH);
digitalWrite(segG, HIGH);
break;

case 8:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, LOW);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;

case 9:
digitalWrite(segA, LOW);
digitalWrite(segB, LOW);
digitalWrite(segC, LOW);
digitalWrite(segD, LOW);
digitalWrite(segE, HIGH);
digitalWrite(segF, LOW);
digitalWrite(segG, LOW);
break;
}
}

Please read "How to use this forum" and post the code properly. Edit your posts to add code tags.

jremington:
Please read "How to use this forum" and post the code properly. Edit your posts to add code tags.

Aright. I will do that.

Hi bro

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.