🔍 Search on DIYTechStudio

Wednesday, May 13, 2020

How to make a custom DIY Temperature Meter using Bar Graph & Atmega328p

Video

In this post, I will show you how to make a custom DIY temperature meter using bar graph & Atmega328p

Below is the circuit diagram which was created in KiCAD for this project which includes parts like 
  • ​1*Atmega328p
  • 1*Bar Graph
  • 1*10K Ohm
  • 10*220 Ohm
  • 1*LM35
  • 1*Terminal Block
  • 2*22pf Ceramic Capacitor
  • 1*16 MHz Crystal
  • 1*Custom Made PCB by ​JLCPCB.com

Theory

The main section of the circuit (Atmega328p, 22pF Ceramic Capacitor, 16MHz Crystal, 10K ohm) is the downsized version of the Arduino UNO

The input section of the circuit is an integrated analog temperature sensor whose electrical output is proportional to Degree Centigrade (LM35). The output of the LM35 is connected to pin 23 of the Atmega328p.


The output section is the remaining section of the circuit which includes Bar graph and 10*220 ohm resistor


Affiliate Links

Atmega328p
Bar Graph
16 MHz Crystal
Next, I created the PCB layout in KiCAD & ordered the PCB board through PCB fabricator JLCPCB.com


Next, I uploaded the below sketch to the Atmega328p
  • Read: How to upload the sketch to an Atmega328p

Sketch Start
float val;
float tempPin = A0;
int ledCount=10;
int thisLed;
int ledPin[] = {
  3, 4, 5, 6, 7, 8, 9, 10, 11, 12
};
int ledON=HIGH;
int ledOFF=LOW;
void setup()
{
Serial.begin(9600);
 for (int thisLed = 0; thisLed < ledCount; thisLed++) {
  pinMode(ledPin[thisLed], OUTPUT);
 }
}
void loop()
{
val = analogRead(tempPin);
float mv = ( val/1024.0)*5000;
float cel = mv/10;
float farh = (cel*9)/5 + 32;
    if (cel >= 25 && cel < 26 ) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledOFF);
          digitalWrite(ledPin[2], ledOFF);
          digitalWrite(ledPin[3], ledOFF);
          digitalWrite(ledPin[4], ledOFF);
          digitalWrite(ledPin[5], ledOFF);
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
    } else if(cel >= 26 && cel < 27) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledOFF);
          digitalWrite(ledPin[3], ledOFF);
          digitalWrite(ledPin[4], ledOFF);
          digitalWrite(ledPin[5], ledOFF);
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
    } else if(cel >= 27 && cel < 28) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledOFF);
          digitalWrite(ledPin[4], ledOFF);
          digitalWrite(ledPin[5], ledOFF);
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
    } else if(cel >= 28 && cel < 29) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledOFF);
          digitalWrite(ledPin[5], ledOFF);
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
    } else if(cel >= 29 && cel < 30) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledON); 
          digitalWrite(ledPin[5], ledOFF);
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
    } else if(cel >= 30 && cel < 31) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledON); 
          digitalWrite(ledPin[5], ledON); 
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
     } else if(cel >= 31 && cel < 32) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledON); 
          digitalWrite(ledPin[5], ledON); 
          digitalWrite(ledPin[6], ledON); 
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
     } else if(cel >= 32 && cel < 33) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledON); 
          digitalWrite(ledPin[5], ledON); 
          digitalWrite(ledPin[6], ledON); 
          digitalWrite(ledPin[7], ledON); 
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
     } else if(cel >= 33 && cel < 34) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledON); 
          digitalWrite(ledPin[5], ledON); 
          digitalWrite(ledPin[6], ledON); 
          digitalWrite(ledPin[7], ledON); 
          digitalWrite(ledPin[8], ledON); 
          digitalWrite(ledPin[9], ledOFF);
     } else if (cel >= 34) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledON); 
          digitalWrite(ledPin[5], ledON); 
          digitalWrite(ledPin[6], ledON); 
          digitalWrite(ledPin[7], ledON); 
          digitalWrite(ledPin[8], ledON); 
          digitalWrite(ledPin[9], ledON); 
     } else if (cel < 25 ) {
          digitalWrite(ledPin[0], ledOFF);
          digitalWrite(ledPin[1], ledOFF);
          digitalWrite(ledPin[2], ledOFF);
          digitalWrite(ledPin[3], ledOFF);
          digitalWrite(ledPin[4], ledOFF);
          digitalWrite(ledPin[5], ledOFF);
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
     }
Serial.print("TEMPRATURE = ");
Serial.print(cel);
Serial.print("*C");
Serial.println();
delay(1000);
/* uncomment this to get temperature in farenhite
Serial.print("TEMPRATURE = ");
Serial.print(farh);
Serial.print("*F");
Serial.println();
*/
}
Sketch End 




Sketch Explanation

The below section of the sketch is where all the variables are declared and initialized

float val;
float tempPin = A0;
int ledCount=10;
int thisLed;
int ledPin[] = {
  3, 4, 5, 6, 7, 8, 9, 10, 11, 12
};
int ledON=HIGH;
int ledOFF=LOW;
void setup()
{
Serial.begin(9600);
 for (int thisLed = 0; thisLed < ledCount; thisLed++) {
  pinMode(ledPin[thisLed], OUTPUT);
 }
}

In this section of the sketch, the analog data is converted to the readable format

val = analogRead(tempPin);   //to read the data from the LM35
float mv = ( val/1024.0)*5000; //Converting the input voltage to digital value (b/w the range 0 to 1023)
float cel = mv/10; //Converting to celcius
float farh = (cel*9)/5 + 32; 
 //Converting to Fahrenheit

In the next section of the sketch, the converted value is passed through a number IF loop to determine how many LED lights should be turned ON

    if (cel >= 25 && cel < 26 ) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledOFF);
          digitalWrite(ledPin[2], ledOFF);
          digitalWrite(ledPin[3], ledOFF);
          digitalWrite(ledPin[4], ledOFF);
          digitalWrite(ledPin[5], ledOFF);
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
    } else if(cel >= 26 && cel < 27) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledOFF);
          digitalWrite(ledPin[3], ledOFF);
          digitalWrite(ledPin[4], ledOFF);
          digitalWrite(ledPin[5], ledOFF);
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
    } else if(cel >= 27 && cel < 28) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledOFF);
          digitalWrite(ledPin[4], ledOFF);
          digitalWrite(ledPin[5], ledOFF);
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
    } else if(cel >= 28 && cel < 29) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledOFF);
          digitalWrite(ledPin[5], ledOFF);
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
    } else if(cel >= 29 && cel < 30) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledON); 
          digitalWrite(ledPin[5], ledOFF);
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
    } else if(cel >= 30 && cel < 31) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledON); 
          digitalWrite(ledPin[5], ledON); 
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
     } else if(cel >= 31 && cel < 32) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledON); 
          digitalWrite(ledPin[5], ledON); 
          digitalWrite(ledPin[6], ledON); 
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
     } else if(cel >= 32 && cel < 33) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledON); 
          digitalWrite(ledPin[5], ledON); 
          digitalWrite(ledPin[6], ledON); 
          digitalWrite(ledPin[7], ledON); 
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
     } else if(cel >= 33 && cel < 34) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledON); 
          digitalWrite(ledPin[5], ledON); 
          digitalWrite(ledPin[6], ledON); 
          digitalWrite(ledPin[7], ledON); 
          digitalWrite(ledPin[8], ledON); 
          digitalWrite(ledPin[9], ledOFF);
     } else if (cel >= 34) {
          digitalWrite(ledPin[0], ledON); 
          digitalWrite(ledPin[1], ledON); 
          digitalWrite(ledPin[2], ledON); 
          digitalWrite(ledPin[3], ledON); 
          digitalWrite(ledPin[4], ledON); 
          digitalWrite(ledPin[5], ledON); 
          digitalWrite(ledPin[6], ledON); 
          digitalWrite(ledPin[7], ledON); 
          digitalWrite(ledPin[8], ledON); 
          digitalWrite(ledPin[9], ledON); 
     } else if (cel < 25 ) {
          digitalWrite(ledPin[0], ledOFF);
          digitalWrite(ledPin[1], ledOFF);
          digitalWrite(ledPin[2], ledOFF);
          digitalWrite(ledPin[3], ledOFF);
          digitalWrite(ledPin[4], ledOFF);
          digitalWrite(ledPin[5], ledOFF);
          digitalWrite(ledPin[6], ledOFF);
          digitalWrite(ledPin[7], ledOFF);
          digitalWrite(ledPin[8], ledOFF);
          digitalWrite(ledPin[9], ledOFF);
     } 

The next section of the sketch is optional. This section is to see the temperature in the built-in serial monitor of the Arduino IDE

Serial.print("TEMPRATURE = ");
Serial.print(cel);
Serial.print("*C");
Serial.println();
delay(1000);
/* uncomment this to get temperature in farenhite
Serial.print("TEMPRATURE = ");
Serial.print(farh);
Serial.print("*F");
Serial.println();
*/
}



After receiving the fabricated PCB board I mounted all the components as per the circuit diagram & tested the circuit 




Comparing my circuit result to a commercially available temperature meter



Thank You For Reading Guys !!!

No comments:

Post a Comment

Hi, Thanks for Commenting !!!