Now that the display is working we can add the TMP36 to read the temperature.
There are 3 pins on the TMP36. The left pin is power, middle pin is analog voltage out and the right pin is ground.
We will connect the power pin to 3V on the Feather. This way it will work when on battery powersince the USB pin will not be powered when on battery only. Analog voltage out will connect to A0 on the Feather. Ground to GND on the Feather.
The code for this part is longer and completely replaces the code from the previous step. I have also included a degree symbol bitmap so it displays the temperature nicely. We can also choose to either display Celsius or Fahrenheit.
// include the library code: #include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins LiquidCrystal lcd(6, 5, 9, 10, 11, 12); // Degree symbol bitmap byte degree[8] = { B01000, B10100, B01000, B00000, B00000, B00000, B00000, B00000, }; //TMP36 Pin Variables const int sensorPin = A0; //the analog pin the TMP36's Vout (sense) pin is connected to //Set to 1 to display Celsius instead of Fahrenheit int celsius = 0; void setup() { //Create the degree symbol bitmap lcd.createChar(0, degree); // set up the LCD's number of columns and rows: lcd.begin(16, 2); } void loop() { //Clear LCD lcd.clear(); //Display Currently on the LCD lcd.print("Currently ") ; //getting the voltage reading from the temperature sensor int reading = analogRead(sensorPin); // converting that reading to voltage, for 3.3v arduino use 3.3 float voltage = reading * 3.3; voltage /= 1024.0; float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree with 500 mV offset //to degrees ((voltage - 500mV) times 100) if (celsius == 1) //If you set temperature as Celsius it will print Celsius values { //Round the temperature to a whole number float roundedTempC = round(temperatureC); // Display temperature in C lcd.print(roundedTempC, 0); lcd.write(byte(0)); //Degree symbol we created earlier lcd.print("C"); } else //Display in Fahrenheit { //Convert from Celsius to Fahrenheit float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0; //Round the temperature to a whole number float roundedTempF = round(temperatureF); // Display temperature in F lcd.print(roundedTempF, 0); lcd.write(byte(0)); //Degree symbol we created earlier lcd.print("F"); } delay(1000); }
If you want to change from Fahrenheit to Celsius change this section in the code.
//Set to 1 to display Celsius instead of Fahrenheit int celsius = 0;
This way displays the temperature in Fahrenheit.
//Set to 1 to display Celsius instead of Fahrenheit int celsius = 1;
With the value now changed to 1 it would display the temperature in Celsius. In the loop section of the program it checks this setting and then chooses which format to display
Now you should have a good temperature reading on the display.
For more information on using the TMP36 check out this guide.
Text editor powered by tinymce.