Four Segment LED

The four Segment LED includes four seven segment LED’s to display 4 values at a time, Since there is only 12 pins needed to display 4 different values, the LED uses a method called Multiplexing, where one digit is shown at a time, but switched fast, so the human eye sees all 4 displays at the same time.

Push Button

The push button will need to be held down in order for the value of the button to change, so in order to make it a toggle button, we will need to use debouncing and define an additional value.

Temperature Sensor

The temperature sensor will use Analog Input in order to read the voltage, instead of the digital input which detects an on/off state. The sensor will work by converting the voltage output into a temperature reading.

A datasheet of the 5461AS LED used can be found here. A datasheet for the TMP36 Temperature sensor can be found here.

To start out, lets declare the pins of our 4 segment LED

const int LEDA = 2;
const int LEDB = 3;
const int LEDC = 4;
const int LEDD = 5;
const int LEDE = 6;
const int LEDF = 7;
const int LEDG = 8;

const int LEDd1 = 9;
const int LEDd2 = 10;
const int LEDd3 = 11;
const int LEDd4 = 12;

const int button = 13; //choose decimal

const int tempPin = 0;

int lastButtonState = LOW;
int currentState = LOW;
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 1000;
bool debouncing = false;

button, tempPin, and the button state variables will be used later

Now, In setup declare our LED’s as output, and the button as input

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(LEDA, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(LEDC, OUTPUT);
  pinMode(LEDD, OUTPUT);
  pinMode(LEDE, OUTPUT);
  pinMode(LEDF, OUTPUT);
  pinMode(LEDG, OUTPUT);

  pinMode(LEDd1, OUTPUT);
  pinMode(LEDd2, OUTPUT);
  pinMode(LEDd3, OUTPUT);
  pinMode(LEDd4, OUTPUT);

  pinMode(button, INPUT);
}

Now, lets create functions for the display, lets create a function to clear the display first.

void emptyDisplay() {
  digitalWrite(LEDA, LOW);
  digitalWrite(LEDB, LOW);
  digitalWrite(LEDC, LOW);
  digitalWrite(LEDD, LOW);
  digitalWrite(LEDE, LOW);
  digitalWrite(LEDF, LOW);
  digitalWrite(LEDG, LOW);
}

Next, lets create a function to choose which display, of the 4 displays, we are controlling.

void chooseDisplay(int x) {
  digitalWrite(LEDd1, HIGH);
  digitalWrite(LEDd2, HIGH);
  digitalWrite(LEDd3, HIGH);
  digitalWrite(LEDd4, HIGH);

  if (x == 1) {
    digitalWrite(LEDd1, LOW);
  }
  if (x == 2) {
    digitalWrite(LEDd2, LOW);
  }
  if (x == 3) {
    digitalWrite(LEDd3, LOW);
  }
  if (x == 4) {
    digitalWrite(LEDd4, LOW);
  }
}

Now lets create a function to display special characters, such as the degrees symbol.

void displaySpecial(int x) {//display special characters, degrees, celcius, fahreneit
  //degrees
  if (x == 0) {
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDF, HIGH);
  }
  //celc
  if (x == 1) {
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDE, HIGH);
    digitalWrite(LEDD, HIGH);
  }
  //fahren
  if (x == 2) {
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDE, HIGH);
  }
}

Now, lets create a function to display numbers 0-9

void displayNumb(int numb) {
  //displays the number passed on the led.
  if (numb == 0) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDC, HIGH);
    digitalWrite(LEDD, HIGH);
    digitalWrite(LEDE, HIGH);
    digitalWrite(LEDF, HIGH);
  }
  
  if (numb == 1) {
    emptyDisplay();
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDC, HIGH);
  }

  if (numb == 2) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDE, HIGH);
    digitalWrite(LEDD, HIGH);
  }

  if (numb == 3) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDC, HIGH);
    digitalWrite(LEDD, HIGH);
  }

  if (numb == 4) {
    emptyDisplay();
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDC, HIGH);
  }

  if (numb == 5) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDC, HIGH);
    digitalWrite(LEDD, HIGH);
  }

  if (numb == 6) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDE, HIGH);
    digitalWrite(LEDC, HIGH);
    digitalWrite(LEDD, HIGH);
  }

  if (numb == 7) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDC, HIGH);
  }

  if (numb == 8) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDC, HIGH);
    digitalWrite(LEDD, HIGH);
    digitalWrite(LEDE, HIGH);
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDG, HIGH);
  }

  if (numb == 9) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDC, HIGH);
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDG, HIGH);
  }
}

Now, lets go to the loop function, first lets start by using our temperature sensor

void loop() {
  int reading = analogRead(tempPin);
  float voltage = (reading * 5.0); //convert reading to voltage
  voltage = (voltage/1024);
  float temperatureC = ((voltage - 0.5) * 100);  //converting from 10 mv per degree with 500 mV offset
  
  if (currentState == 1) { //if button pressed, show fahrenheit, convert celcius to fahrenheit
    temperatureC = ((temperatureC * 9 / 5) + 32);
  }
  
  int first_number = (int(temperatureC / 10));
  int second_number = (int((float(temperatureC / 10) - int(temperatureC / 10)) * 10));
}

This code converts the voltage which the temperature pin outputs into temperature in celcius, currentState will be defined with the button next, normally it is equal to 0

Next, lets define the debouncing, we need this so when the button is pressed, it does not change back instantly.

void loop() {
  int reading = analogRead(tempPin);
  float voltage = (reading * 5.0); //convert reading to voltage
  voltage = (voltage/1024);
  float temperatureC = ((voltage - 0.5) * 100);  //converting from 10 mv per degree with 500 mV offset
  
  if (currentState == 1) { //if button pressed, show fahrenheit, convert celcius to fahrenheit
    temperatureC = ((temperatureC * 9 / 5) + 32);
  }
  
  int first_number = (int(temperatureC / 10));
  int second_number = (int((float(temperatureC / 10) - int(temperatureC / 10)) * 10));
  
  if (digitalRead(button) != lastButtonState) {
    if ( debouncing == false) {
      // Detect change              
      // reset the debouncing timer   
      // Announce we are debouncing  
      lastDebounceTime = millis(); //reset debounce timer
      currentState = digitalRead(button);
      debouncing = true;
    }
  }

  if (debouncing == true) {//if debouncing
    if ((millis() - lastDebounceTime) > debounceDelay) {
      int buttonState = digitalRead(button); 

      // Still the button is in the same state
      if (buttonState == currentState) {
        debouncing = false;
        lastButtonState = digitalRead(button);
      }
    }
  }
}

To explain this code, first the lastDebounceTime is reset from after a button is pressed, then changes currentState and debouncing to true, if it is true and its been longer than the debounce delay, then the buttonstate is changed again, then the debouncing variable is changed back to false.

Now, since we have all the data needed, and debounced the push button, we can start displaying these values.

void loop() {
  int reading = analogRead(tempPin);
  float voltage = (reading * 5.0); //convert reading to voltage
  voltage = (voltage/1024);
  float temperatureC = ((voltage - 0.5) * 100);  //converting from 10 mv per degree with 500 mV offset
  
  if (currentState == 1) { //if button pressed, show fahrenheit, convert celcius to fahrenheit
    temperatureC = ((temperatureC * 9 / 5) + 32);
  }
  
  int first_number = (int(temperatureC / 10));
  int second_number = (int((float(temperatureC / 10) - int(temperatureC / 10)) * 10));
  


  if (digitalRead(button) != lastButtonState) {
    if ( debouncing == false) {
      // Detect change              
      // reset the debouncing timer   
      // Announce we are debouncing  
      lastDebounceTime = millis(); //reset debounce timer
      currentState = digitalRead(button);
      debouncing = true;
    }
  }

  if (debouncing == true) {//if debouncing
    if ((millis() - lastDebounceTime) > debounceDelay) {
      int buttonState = digitalRead(button); //button state = digitalreadbutton

      // Still the button is in the same state
      if (buttonState == currentState) {
        debouncing = false;
        lastButtonState = digitalRead(button);
      }
    }
  }
  
  //display
  emptyDisplay();
  chooseDisplay(1);
  displayNumb(first_number);

  emptyDisplay();
  chooseDisplay(2);
  displayNumb(second_number);

  emptyDisplay();
  chooseDisplay(3);
  displaySpecial(0);

  emptyDisplay();
  chooseDisplay(4);
  if (currentState == 1) {
    displaySpecial(2);
  }else {
    displaySpecial(1);
  }
}

The final code should look somewhat like this.

const int LEDA = 2;
const int LEDB = 3;
const int LEDC = 4;
const int LEDD = 5;
const int LEDE = 6;
const int LEDF = 7;
const int LEDG = 8;

const int LEDd1 = 9;
const int LEDd2 = 10;
const int LEDd3 = 11;
const int LEDd4 = 12;

const int button = 13; //choose decimal

const int tempPin = 0;

int lastButtonState = LOW;
int currentState = LOW;
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 1000;
bool debouncing = false;


void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(LEDA, OUTPUT);
  pinMode(LEDB, OUTPUT);
  pinMode(LEDC, OUTPUT);
  pinMode(LEDD, OUTPUT);
  pinMode(LEDE, OUTPUT);
  pinMode(LEDF, OUTPUT);
  pinMode(LEDG, OUTPUT);

  pinMode(LEDd1, OUTPUT);
  pinMode(LEDd2, OUTPUT);
  pinMode(LEDd3, OUTPUT);
  pinMode(LEDd4, OUTPUT);

  pinMode(button, INPUT);
}


void loop() {
  int reading = analogRead(tempPin);
  float voltage = (reading * 5.0); //convert reading to voltage
  voltage = (voltage/1024);
  float temperatureC = ((voltage - 0.5) * 100);  //converting from 10 mv per degree with 500 mV offset
  
  if (currentState == 1) { //if button pressed, show fahrenheit, convert celcius to fahrenheit
    temperatureC = ((temperatureC * 9 / 5) + 32);
  }
  
  int first_number = (int(temperatureC / 10));
  int second_number = (int((float(temperatureC / 10) - int(temperatureC / 10)) * 10));
  


  if (digitalRead(button) != lastButtonState) {
    if ( debouncing == false) {
      // Detect change              
      // reset the debouncing timer   
      // Announce we are debouncing  
      lastDebounceTime = millis(); //reset debounce timer
      currentState = digitalRead(button);
      debouncing = true;
    }
  }

  if (debouncing == true) {//if debouncing
    if ((millis() - lastDebounceTime) > debounceDelay) {
      int buttonState = digitalRead(button); //button state = digitalreadbutton

      // Still the button is in the same state
      if (buttonState == currentState) {
        debouncing = false;
        lastButtonState = digitalRead(button);
      }
    }
  }
  
  //display
  emptyDisplay();
  chooseDisplay(1);
  displayNumb(first_number);

  emptyDisplay();
  chooseDisplay(2);
  displayNumb(second_number);

  emptyDisplay();
  chooseDisplay(3);
  displaySpecial(0);

  emptyDisplay();
  chooseDisplay(4);
  if (currentState == 1) {
    displaySpecial(2);
  }else {
    displaySpecial(1);
  }
}



void displaySpecial(int x) {//display special characters, degrees, celcius, fahreneit
  //degrees
  if (x == 0) {
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDF, HIGH);
  }
  //celc
  if (x == 1) {
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDE, HIGH);
    digitalWrite(LEDD, HIGH);
  }
  //fahren
  if (x == 2) {
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDE, HIGH);
  }
}

void emptyDisplay() {
  digitalWrite(LEDA, LOW);
  digitalWrite(LEDB, LOW);
  digitalWrite(LEDC, LOW);
  digitalWrite(LEDD, LOW);
  digitalWrite(LEDE, LOW);
  digitalWrite(LEDF, LOW);
  digitalWrite(LEDG, LOW);
}

void chooseDisplay(int x) {
  digitalWrite(LEDd1, HIGH);
  digitalWrite(LEDd2, HIGH);
  digitalWrite(LEDd3, HIGH);
  digitalWrite(LEDd4, HIGH);

  if (x == 1) {
    digitalWrite(LEDd1, LOW);
  }
  if (x == 2) {
    digitalWrite(LEDd2, LOW);
  }
  if (x == 3) {
    digitalWrite(LEDd3, LOW);
  }
  if (x == 4) {
    digitalWrite(LEDd4, LOW);
  }
}

void displayNumb(int numb) {
  //displays the number passed on the led.
  if (numb == 0) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDC, HIGH);
    digitalWrite(LEDD, HIGH);
    digitalWrite(LEDE, HIGH);
    digitalWrite(LEDF, HIGH);
  }
  
  if (numb == 1) {
    emptyDisplay();
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDC, HIGH);
  }

  if (numb == 2) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDE, HIGH);
    digitalWrite(LEDD, HIGH);
  }

  if (numb == 3) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDC, HIGH);
    digitalWrite(LEDD, HIGH);
  }

  if (numb == 4) {
    emptyDisplay();
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDC, HIGH);
  }

  if (numb == 5) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDC, HIGH);
    digitalWrite(LEDD, HIGH);
  }

  if (numb == 6) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDG, HIGH);
    digitalWrite(LEDE, HIGH);
    digitalWrite(LEDC, HIGH);
    digitalWrite(LEDD, HIGH);
  }

  if (numb == 7) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDC, HIGH);
  }

  if (numb == 8) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDC, HIGH);
    digitalWrite(LEDD, HIGH);
    digitalWrite(LEDE, HIGH);
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDG, HIGH);
  }

  if (numb == 9) {
    emptyDisplay();
    digitalWrite(LEDA, HIGH);
    digitalWrite(LEDB, HIGH);
    digitalWrite(LEDC, HIGH);
    digitalWrite(LEDF, HIGH);
    digitalWrite(LEDG, HIGH);
  }
}
Categories: Arduino