Arduino Distance and Temperature Sensor + 7 Segment Display

by 724076 in Circuits > Arduino

1235 Views, 4 Favorites, 0 Comments

Arduino Distance and Temperature Sensor + 7 Segment Display

1.PNG

This project was something that I had thought about doing for a long time to make something that is relatively simple into something that is complicated just by adding a few extra parts. About a year ago I originally made a distance sensor with a single 7 segment display that would display the distance in inches. This is a better version of that which is more accurate and is in centimetres. I chose a max distance it could show of 99 cm because that's about as far as I've seen the distance sensor be accurate to in my experience.

Supplies

- An Arduino

- 2x 7 Segment Displays (The ones I used are common Anode)

- 2x 1K Ohm Resistor

- A TMP36 Temperature Sensor

- An Ultrasonic Distance Sensor

Assembling the Circuit

Arduino Distance and Temperature Sensor + 7 Segment Display - Build
3.PNG

I'll explain the way that the 2 7 Seg displays work when I go over the code. The general idea is that the same terminals a-f are connected between the two displays and the Arduino, the exception are the common pins which are also attached to the Arduino.

The temperature sensor is much simpler as you just need to attach one end into power, the other into ground and the center one into an Analog In (I used A0).

The distance sensor just needs power, ground, a digital in and digital out.

The Code

2.PNG
4.PNG

The code for this is complex but not difficult to understand if you know the basics of how a 7 Seg display works.

The basic idea of using the 2 displays is that they cannot be on for at the same moment or else the same value would be displayed on both. However, if we have it constantly changing from off to on so fast that we can't see, it would seem to the human eye to be on.

This is the code that I used:

int aPin = 2; //Variables
int bPin = 3;
int cPin = 4;
int dPin = 5;
int ePin = 6;
int fPin = 7;
int gPin = 8;
int firstPin = 9;
int secondPin = 10;
int trigPin = 11;
int echoPin = 12;

long duration, cm;
int reading;
float voltage = 0, temperature = 0, change, secondVal;

void setup()
{
  pinMode(aPin, OUTPUT);
  pinMode(bPin, OUTPUT);
  pinMode(cPin, OUTPUT);
  pinMode(dPin, OUTPUT);
  pinMode(ePin, OUTPUT);
  pinMode(fPin, OUTPUT);
  pinMode(gPin, OUTPUT);
  pinMode(firstPin, OUTPUT);
  pinMode(secondPin, OUTPUT);
  
  // Ultrasonic Setup
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  
  // Temperature Sensor
  pinMode(A0, INPUT);
  
}


void loop()
{
  
  // Temperature Sensor
  temperature = map(((analogRead(A0) - 20) * 3.04), 0, 1023, -40, 125);
  
  // Distance Sensor
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  
  cm = microsecondsToCentimeters(duration, temperature); //Uses the function to determine the distance
  
  if (cm>=0&&cm<=9){
   	digitalWrite(firstPin, HIGH); // Turns on the Display
  	zero(); // Sets the value to display
  	delay(5); // Leaves it on for 5 miliseconds
  	digitalWrite(firstPin, LOW); // Turns the display off
  	reset(); // Resets the Display
    change = 0; // Helps calculate the next digit
  }
  else if (cm>=10&&cm<=19){
    digitalWrite(firstPin, HIGH);
  	one();
  	delay(5);
  	digitalWrite(firstPin, LOW);
  	reset();
    change = 10;
  }
  else if (cm>=20&&cm<=29){
    digitalWrite(firstPin, HIGH);
  	two();
  	delay(5);
  	digitalWrite(firstPin, LOW);
  	reset();
    change = 20;
  }
  else if (cm>=30&&cm<=39){
    digitalWrite(firstPin, HIGH);
  	three();
  	delay(5);
  	digitalWrite(firstPin, LOW);
  	reset();
    change = 30;
  }
  else if (cm>=40&&cm<=49){
    digitalWrite(firstPin, HIGH);
  	four();
  	delay(5);
  	digitalWrite(firstPin, LOW);
  	reset();
    change = 40;
  }
  else if (cm>=50&&cm<=59){
    digitalWrite(firstPin, HIGH);
  	five();
  	delay(5);
  	digitalWrite(firstPin, LOW);
  	reset();
    change = 50;
  }
  else if (cm>=60&&cm<=69){
    digitalWrite(firstPin, HIGH);
  	six();
  	delay(5);
  	digitalWrite(firstPin, LOW);
  	reset();
    change = 60;
  }
  else if (cm>=70&&cm<=79){
    digitalWrite(firstPin, HIGH);
  	seven();
  	delay(5);
  	digitalWrite(firstPin, LOW);
  	reset();
    change = 70;
  }
  else if (cm>=80&&cm<=89){
    digitalWrite(firstPin, HIGH);
  	eight();
  	delay(5);
  	digitalWrite(firstPin, LOW);
  	reset();
    change = 80;
  }
  else if (cm>=90&&cm<=99){
    digitalWrite(firstPin, HIGH);
  	nine();
  	delay(5);
  	digitalWrite(firstPin, LOW);
  	reset();
    change = 90;
  }
  else{
    digitalWrite(firstPin, HIGH);
  	digitalWrite (gPin, LOW);
  	delay(5);
  	digitalWrite(firstPin, LOW);
  	reset();
    change = 200;
  }
  
  secondVal = cm - change;
  
  if (secondVal==0){
    digitalWrite(secondPin, HIGH);
  	zero();
  	delay(5);
  	digitalWrite(secondPin, LOW);
  	reset();
  }
  else if (secondVal==1){
    digitalWrite(secondPin, HIGH);
  	one();
  	delay(5);
  	digitalWrite(secondPin, LOW);
  	reset();
  }
  else if (secondVal==2){
    digitalWrite(secondPin, HIGH);
  	two();
  	delay(5);
  	digitalWrite(secondPin, LOW);
  	reset();
  }
  else if (secondVal==3){
    digitalWrite(secondPin, HIGH);
  	three();
  	delay(5);
  	digitalWrite(secondPin, LOW);
  	reset();
  }
  else if (secondVal==4){
    digitalWrite(secondPin, HIGH);
  	four();
  	delay(5);
  	digitalWrite(secondPin, LOW);
  	reset();
  }
  else if (secondVal==5){
    digitalWrite(secondPin, HIGH);
  	five();
  	delay(5);
  	digitalWrite(secondPin, LOW);
  	reset();
  }
  else if (secondVal==6){
    digitalWrite(secondPin, HIGH);
  	six();
  	delay(5);
  	digitalWrite(secondPin, LOW);
  	reset();
  }
  else if (secondVal==7){
    digitalWrite(secondPin, HIGH);
  	seven();
  	delay(5);
  	digitalWrite(secondPin, LOW);
  	reset();
  }
  else if (secondVal==8){
    digitalWrite(secondPin, HIGH);
  	eight();
  	delay(5);
  	digitalWrite(secondPin, LOW);
  	reset();
  }
  else if (secondVal==9){
    digitalWrite(secondPin, HIGH);
  	nine();
  	delay(5);
  	digitalWrite(secondPin, LOW);
  	reset();
  }
  else{
    digitalWrite(secondPin, HIGH);
  	digitalWrite (gPin, LOW);
  	delay(5);
  	digitalWrite(secondPin, LOW);
  	reset();
  }
  
}

long microsecondsToCentimeters(long microseconds, float temp){
  // The speed of sound is 331.4 0.6*Temperature (in m/s).
  // The value 0.0001 is used to go from m/s to cm/microsecond.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return (microseconds*0.0001*(331.4+0.6*temp))/2;
}

void reset (){
  digitalWrite (aPin, HIGH);
  digitalWrite (bPin, HIGH);
  digitalWrite (cPin, HIGH);
  digitalWrite (dPin, HIGH);
  digitalWrite (ePin, HIGH);
  digitalWrite (fPin, HIGH);
  digitalWrite (gPin, HIGH);
  
}

void nine (){
  digitalWrite (aPin, LOW);
  digitalWrite (bPin, LOW);
  digitalWrite (cPin, LOW);
  digitalWrite (fPin, LOW);
  digitalWrite (gPin, LOW);
}

void eight (){
  digitalWrite (aPin, LOW);
  digitalWrite (bPin, LOW);
  digitalWrite (cPin, LOW);
  digitalWrite (dPin, LOW);
  digitalWrite (ePin, LOW);
  digitalWrite (fPin, LOW);
  digitalWrite (gPin, LOW);
}

void seven(){
  digitalWrite (aPin, LOW);
  digitalWrite (bPin, LOW);
  digitalWrite (cPin, LOW);
}

void six(){
  digitalWrite (aPin, LOW);
  digitalWrite (cPin, LOW);
  digitalWrite (dPin, LOW);
  digitalWrite (ePin, LOW);
  digitalWrite (fPin, LOW);
  digitalWrite (gPin, LOW);
}

void five(){
  digitalWrite (aPin, LOW);
  digitalWrite (cPin, LOW);
  digitalWrite (dPin, LOW);
  digitalWrite (fPin, LOW);
  digitalWrite (gPin, LOW);
}

void four(){
  digitalWrite (bPin, LOW);
  digitalWrite (cPin, LOW);
  digitalWrite (fPin, LOW);
  digitalWrite (gPin, LOW);
}

void three(){
  digitalWrite (aPin, LOW);
  digitalWrite (bPin, LOW);
  digitalWrite (cPin, LOW);
  digitalWrite (dPin, LOW);
  digitalWrite (gPin, LOW);
}

void two(){
  digitalWrite (aPin, LOW);
  digitalWrite (bPin, LOW);
  digitalWrite (dPin, LOW);
  digitalWrite (ePin, LOW);
  digitalWrite (gPin, LOW);
}

void one(){
  digitalWrite (bPin, LOW);
  digitalWrite (cPin, LOW);
}

void zero(){
  digitalWrite (aPin, LOW);
  digitalWrite (bPin, LOW);
  digitalWrite (cPin, LOW);
  digitalWrite (dPin, LOW);
  digitalWrite (ePin, LOW);
  digitalWrite (fPin, LOW);
}