Sharp PM2.5 dust sensor and Blynk

Hi everyone! been using Blynk for a few months now and love the ease of use, but… I’m having issues with this code. just using the code to read the sensor works fine, but when I inject the Blynk code it doesn’t show the correct readings(maxed out) or display anything on the LCD. I had help to edit the original code (first one) too the second one but have been unable to resolve the issue. I am using a NodeMCU32s and Arduino IDE with Blynk on iOS. I have learning difficulties that makes it difficult for me to learn by sitting down and reading up, I learn by when I encounter issues but this has me stumped.

//First code
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); 
int measurePin = 4; 
int ledPower = 2;  
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
void setup(){
  lcd.init();                      
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("Raw Signal Value: ");
  lcd.setCursor(0,2);
  lcd.print("Voltage:");
  lcd.setCursor(0,3);
  lcd.print("Dust Density:");
  pinMode(ledPower,OUTPUT);
}
void loop(){
  digitalWrite(ledPower,LOW); 
  delayMicroseconds(samplingTime);
  voMeasured = analogRead(measurePin);
  delayMicroseconds(deltaTime);
  digitalWrite(ledPower,HIGH); // turn the LED off
  delayMicroseconds(sleepTime);
  calcVoltage = voMeasured * (5.0 / 1024.0);
  dustDensity = 0.17 * calcVoltage - 0.1;
  lcd.setCursor(1,1);
  lcd.print(voMeasured);
  lcd.setCursor(9,2);
  lcd.print(calcVoltage);
  lcd.setCursor(14,3);
  lcd.print(dustDensity);
  delay(1000);
}

//Second code for blynk

#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
BlynkTimer timer;
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27); 
char auth[] = "xxx";
char ssid[] = "xxx";
char pass[] = "xxx";
int measurePin = 4;
int ledPower = 2;
int samplingTime = 280;
int deltaTime = 40;
int sleepTime = 9680;
float voMeasured = 0;
float calcVoltage = 0;
float dustDensity = 0;
 
void myTimerEvent(){            //Put your loop instruction in here with the Virtual pins at the end
    digitalWrite(ledPower,LOW);
    delayMicroseconds(samplingTime);
    voMeasured = analogRead(measurePin);
    delayMicroseconds(deltaTime);
    digitalWrite(ledPower,HIGH);
    delayMicroseconds(sleepTime);
    calcVoltage = voMeasured * (5.0 / 1024.0);
    dustDensity = 0.17 * calcVoltage - 0.1;    
    Blynk.virtualWrite(V20,voMeasured );
    Blynk.virtualWrite(V21, calcVoltage);
    Blynk.virtualWrite(V22, dustDensity);
    ifPrint();
}
void ifPrint(){
    lcd.setCursor(1,1);
    lcd.print(voMeasured);
    lcd.setCursor(9,2);
    lcd.print(calcVoltage);
    lcd.setCursor(14,3);
    lcd.print(dustDensity);
}   
void setup(){
    Blynk.begin(auth, ssid, pass);
    lcd.begin(20,4); 
    lcd.backlight();
    lcd.setCursor(0,0);
    lcd.print("Raw Signal Value: ");
    lcd.setCursor(0,2);
    lcd.print("Voltage:");
    lcd.setCursor(0,3);
    lcd.print("Dust Density:");
    pinMode(ledPower,OUTPUT);
    timer.setInterval(1000L, myTimerEvent);   //This sets the time between samples
}
void loop(){
  Blynk.run();
  timer.run();
}

Analog pin 4 is an ADC2 channel pin…

Analog to Digital Converter (ADC)

The ESP32 has 18 x 12 bits ADC input channels (while the ESP8266 only has 1x 10 bits ADC). These are the GPIOs that can be used as ADC and respective channels:

  • ADC1_CH0 (GPIO 36)
  • ADC1_CH1 (GPIO 37)
  • ADC1_CH2 (GPIO 38)
  • ADC1_CH3 (GPIO 39)
  • ADC1_CH4 (GPIO 32)
  • ADC1_CH5 (GPIO 33)
  • ADC1_CH6 (GPIO 34)
  • ADC1_CH7 (GPIO 35)
  • ADC2_CH0 (GPIO 4)
  • ADC2_CH1 (GPIO 0)
  • ADC2_CH2 (GPIO 2)
  • ADC2_CH3 (GPIO 15)
  • ADC2_CH4 (GPIO 13)
  • ADC2_CH5 (GPIO 12)
  • ADC2_CH6 (GPIO 14)
  • ADC2_CH7 (GPIO 27)
  • ADC2_CH8 (GPIO 25)
  • ADC2_CH9 (GPIO 26)

Note: ADC2 pins cannot be used when Wi-Fi is used. So, if you’re using Wi-Fi and you’re having trouble getting the value from an ADC2 GPIO, you may consider using an ADC1 GPIO instead, that should solve your problem.

Pete.

@PeteKnight thank you for your reply! I shall try this now, thank you for taking the time to help.

@PeteKnight I tried your suggestion and it now writes the data to Blynk thank you. Still not having any joy with the LCD though.

Any reason for the difference?

Pete.

@PeteKnight sorry that function (20,4) was added back in and still no joy. This was for someone else that tried to help and had a different library for the I2C LCD

You also seem to have some other issues/inconsistencies with your LCD stuff…

No lcd.init(); in your Blynk sketch, and this odd line instead…

You should take some time to go through your code rather than relying on others to do it for you.

Pete.

@PeteKnight it’s not that easy for me to learn new things. But thank you for your help

Line by line comparisons, or copying and pasting key sections of code from one sketch to another shouldn’t be difficult.

Pete.