Can't get lm35 sensor data from ADC1 of esp32

Hi, I am using ESP32 for read gas sensor and temperature data and send to blynk server as well as Serial Monitor and lcd display. I can read only Gas sensor data but temperature sensor read only zero. I double check the hardware connection. Here are my code


int gasPin=33;
int lm35=34;
// Setup adxl335 x, y , z pin
/*int xAxis = 32;
int yAxis = 35;
int zAxis = 39;*/
int buzzer = 5;
//int Switch = 15;

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define BLYNK_TEMPLATE_ID "TMPL0sHFSboC"
#define BLYNK_TEMPLATE_NAME "AI DRIVEN"
#define BLYNK_AUTH_TOKEN "Cl0H_xKNnQbqFGgpalOmjnlCxpxwTkl"

#define BLYNK_PRINT Serial
#include <WiFi.h>
//#include <ESP8266WiFi.h> 
#include <BlynkSimpleEsp32.h>



//#include <DHT.h>


//char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "HI";  // type your wifi name
char pass[] = "00000000";  // type your wifi password

BlynkTimer timer;

void setup()
{   
  pinMode(buzzer,OUTPUT);
  Serial.begin(115200);
  lcd.begin();                       // Initialize the LCD
  lcd.backlight();                  // Turn on the backlight
  lcd.clear();
  //*********0123456789012345
  lcd.print("Drink L:");
  lcd.print("  Temp:");


  

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  //dht.begin();
  timer.setInterval(1000L, sendSensor);
  digitalWrite(buzzer,HIGH);
  delay(500);
  digitalWrite(buzzer, LOW);
 
  }

void loop()
{
 Blynk.run();
 timer.run();
 }
 void sendSensor(){
  float gasLevel = analogRead(gasPin);
  float temperature = analogRead(lm35);
  Serial.print(temperature);

  // Convert sensor readings to meaningful values
  gasLevel = map(gasLevel, 0, 4095, 0, 100);   // Assuming 0-100% range for gas sensor
  temperature = (temperature * 0.080586);    // (0.080586 )Convert LM35 output to Celsius (3.3V/4095)

  // Send sensor data to Blynk app
  Serial.print("Gas: ");
  Serial.print(gasLevel);
  lcd.setCursor(2,1);
  lcd.print(gasLevel);
  Serial.print("   Temperature: ");
  Serial.println(temperature);
  lcd.setCursor(11,1);
  lcd.print(temperature);
  Blynk.virtualWrite(V0, gasLevel);         // Display gas level on V0
  Blynk.virtualWrite(V1, temperature);       // Display temperature on V1
 }

And my second problem is that Serial monitor and lcd display shows data when device connected to wifi, until wifi connected i can’t get data offline, even buzzer not beep. What should I change to my code for get offline dat when no wifi available. I also try below this and it does not work.

//Tech Trends Shameer
int gasPin=33;
int lm35=34;
// Setup adxl335 x, y , z pin
/*int xAxis = 32;
int yAxis = 35;
int zAxis = 39;*/
int buzzer = 5;
//int Switch = 15;

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define BLYNK_TEMPLATE_ID "TMPL0sHFSboC"
#define BLYNK_TEMPLATE_NAME "AI DRIVEN"
#define BLYNK_AUTH_TOKEN "Cl0H_xKNnQbqFGgpalOmjnlCxpxwTkl"

#define BLYNK_PRINT Serial
#include <WiFi.h>
//#include <ESP8266WiFi.h> 
#include <BlynkSimpleEsp32.h>



//#include <DHT.h>


//char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "HI";  // type your wifi name
char pass[] = "00000000";  // type your wifi password

BlynkTimer timer;

void setup()
{   
  pinMode(buzzer,OUTPUT);
  Serial.begin(115200);
  lcd.begin();                       // Initialize the LCD
  lcd.backlight();                  // Turn on the backlight
  lcd.clear();
  //*********0123456789012345
  lcd.print("Drink L:");
  lcd.print("  Temp:");


  

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  //dht.begin();
  timer.setInterval(1000L, sendSensor);
  digitalWrite(buzzer,HIGH);
  delay(500);
  digitalWrite(buzzer, LOW);
 
  }

void loop()
{
 Blynk.run();
 timer.run();
 sensorData();
 }
 void sendSensor(){
  float gasLevel = analogRead(gasPin);
  float temperature = analogRead(lm35);
  //Serial.print(temperature);

  // Convert sensor readings to meaningful values
  gasLevel = map(gasLevel, 0, 4095, 0, 100);   // Assuming 0-100% range for gas sensor
  temperature = (temperature * 0.080586);    // (0.080586 )Convert LM35 output to Celsius (3.3V/4095)

  /*// Send sensor data to Blynk app
  Serial.print("Gas: ");
  Serial.print(gasLevel);
  lcd.setCursor(2,1);
  lcd.print(gasLevel);
  Serial.print("   Temperature: ");
  Serial.println(temperature);
  lcd.setCursor(11,1);
  lcd.print(temperature);*/
  Blynk.virtualWrite(V0, gasLevel);         // Display gas level on V0
  Blynk.virtualWrite(V1, temperature);       // Display temperature on V1
 }
 void sensorData(){
  float gasLevel = analogRead(gasPin);
  float temperature = analogRead(lm35);
  Serial.print(temperature);

  // Convert sensor readings to meaningful values
  gasLevel = map(gasLevel, 0, 4095, 0, 100);   // Assuming 0-100% range for gas sensor
  temperature = (temperature * 0.080586);    // (0.080586 )Convert LM35 output to Celsius (3.3V/4095)

  // Send sensor data to Blynk app
  Serial.print("Gas: ");
  Serial.print(gasLevel);
  lcd.setCursor(2,1);
  lcd.print(gasLevel);
  Serial.print("   Temperature: ");
  Serial.println(temperature);
  lcd.setCursor(11,1);
  lcd.print(temperature);
  delay(250);
 }

You need to start by taking this out of your void loop, and combining your sendSensor and sensordata functions into one function which is called by BlynkTimer.

Also, when you post serial output, copy and paste the text from your serial monitor and use triple backticks the same as when you post code, rather than posting screenshots.

Blynk.begin() is a blocking function, so you need to stop using it and manually manage your own WiFi connection then use Blynk.config() and Blynk.connect().
Search for these terms and you’ll find examples of how to do this.

Pete.

Please see my first coding where i only use timer.run(); but it is not working.

I would suggest that you post one sketch, and the corresponding serial monitor output, along with any explanatory information (how your sensors are wired, powered etc along with what you’re seeing in Blynk) in a single post if you want assistance with your issue.

Pete.

1 Like
int gasPin=33;
int lm35=34;
// Setup adxl335 x, y , z pin
/*int xAxis = 32;
int yAxis = 35;
int zAxis = 39;*/
int buzzer = 5;
//int Switch = 15;

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

#define BLYNK_TEMPLATE_ID "TMPL0sTISboC"
#define BLYNK_TEMPLATE_NAME "AI DRIVEN"
#define BLYNK_AUTH_TOKEN "Cl0H_xKNnQbqFcHbalpOmjnlCxpxwTkl"

#define BLYNK_PRINT Serial
#include <WiFi.h>
//#include <ESP8266WiFi.h> 
#include <BlynkSimpleEsp32.h>



//#include <DHT.h>


//char auth[] = BLYNK_AUTH_TOKEN;

char ssid[] = "HI";  // type your wifi name
char pass[] = "00000000";  // type your wifi password

BlynkTimer timer;

void setup()
{   
  pinMode(buzzer,OUTPUT);
  Serial.begin(115200);
  lcd.begin();                       // Initialize the LCD
  lcd.backlight();                  // Turn on the backlight
  lcd.clear();
  //*********0123456789012345
  lcd.print("Drink L:");
  lcd.print("  Temp:");


  

  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  //dht.begin();
  timer.setInterval(1000L, sendSensor);
  digitalWrite(buzzer,HIGH);
  delay(500);
  digitalWrite(buzzer, LOW);
 
  }

void loop()
{
 Blynk.run();
 timer.run();
 }
 void sendSensor(){
  float gasLevel = analogRead(gasPin);
  float temperature = analogRead(lm35);
  Serial.print(temperature);

  // Convert sensor readings to meaningful values
  gasLevel = map(gasLevel, 0, 4095, 0, 100);   // Assuming 0-100% range for gas sensor
  temperature = (temperature * 0.080586);    // (0.080586 )Convert LM35 output to Celsius (3.3V/4095)

  // Send sensor data to Blynk app
  Serial.print("Gas: ");
  Serial.print(gasLevel);
  lcd.setCursor(2,1);
  lcd.print(gasLevel);
  Serial.print("   Temperature: ");
  Serial.println(temperature);
  lcd.setCursor(11,1);
  lcd.print(temperature);
  Blynk.virtualWrite(V0, gasLevel);         // Display gas level on V0
  Blynk.virtualWrite(V1, temperature);       // Display temperature on V1
 }

And here are the serial monitor data.

19:10:33.459 -> [1131] Connecting to HI
19:10:35.056 -> [2723] Connected to WiFi
19:10:35.056 -> [2723] IP: 192.168.191.218
19:10:35.056 -> [2724] 
19:10:35.056 ->     ___  __          __
19:10:35.093 ->    / _ )/ /_ _____  / /__
19:10:35.093 ->   / _  / / // / _ \/  '_/
19:10:35.093 ->  /____/_/\_, /_//_/_/\_\
19:10:35.093 ->         /___/ v1.3.2 on ESP32
19:10:35.093 -> 
19:10:35.093 ->  #StandWithUkraine    https://bit.ly/swua
19:10:35.093 -> 
19:10:35.093 -> 
19:10:35.093 -> [2734] Connecting to blynk.cloud:80
19:10:35.456 -> [3104] Ready (ping: 204ms).
19:10:36.525 -> 0.00Gas: 17.00   Temperature: 0.00
19:10:37.506 -> 0.00Gas: 18.00   Temperature: 0.00
19:10:38.529 -> 0.00Gas: 18.00   Temperature: 0.00
19:10:39.532 -> 0.00Gas: 17.00   Temperature: 0.00
19:10:40.510 -> 0.00Gas: 17.00   Temperature: 0.00
19:10:41.527 -> 0.00Gas: 18.00   Temperature: 0.00
19:10:42.517 -> 0.00Gas: 18.00   Temperature: 0.00
19:10:43.533 -> 0.00Gas: 18.00   Temperature: 0.00

And I connect sensor according to this diagram.
LM35-Sensor-1-300x262

If you’re using one of the later variations on the ESP32 Devkit board then that second pin up on the left hand side is actually the CMD pin (GPIO11) which is sometimes mis-labelled as GND.
Try using one of the other GND pins on the board.

Pete.

1 Like

Ok, its working now, thank you.

1 Like