I want to use serial plotter feature of Arduino by using DHT11 as humidity sensor at a time. But I'm getting problem as plotter is not loading by the code I have. Please help me what is the problem because of which serial plotter is not loading.
Here is the Code I wrote:-
#include <dht.h>
#define dht_apin0 A0 // Analog Pin sensor is connected to
dht DHT0;
void setup() {
Serial.begin(9600);
delay(500);//Delay to let system boot
Serial.println("DHT11 Humidity Sensor\n");
delay(1000);//Wait before accessing Sensor
}//end "setup()"
void loop(){
//Start of Program
DHT0.read11(dht_apin0);
Serial.print("Current humidity = ");
Serial.print(DHT0.humidity);
Serial.print("% ");
delay(5000);//Wait 5 seconds before accessing sensor again.
//Fastest should be once every two seconds.
}// end loop()
1 Answer 1
You can't send arbitrary information (percent signs, text etc) to the plotter. The plotter only understands numeric values. You have to separate these number values from each other with commas, then do a println
in your last value:
Serial.print(percent);
Serial.print(F(","));
Serial.print(tankPercent(depth));
Serial.print(F(","));
Serial.print(litres);
Serial.print(F(","));
Serial.println(tankLitres(depth));
In your case, it'd be simply:
Serial.println(DHT0.humidity);
You can go a bit further if you want to switch between either serial output, or serial plotter. In one case, I just set a constant variable for graph or plain data, then have a function for each (my above Serial
statements are actually within a function called displayGraph()
). The data function would be something like you had originally, example:
void displayData(float depth, float percent, float litres){
// water level depth (inches)
Serial.print(F("current missing inches: "));
Serial.println(mixtureDistance());
// cubic inches
Serial.print(F("total cubic inches: "));
Serial.println(TANK_TOTAL_CUBIC_INCHES);
Serial.print(F("current cubic inches: "));
Serial.println(tankCurrentCubicInches(depth));
// litres
Serial.print(F("total litres: "));
Serial.println(TANK_TOTAL_LITRES);
// ... etc
}
I also have projects where I use a switch that if on, sets a flag to output in serial graph format, and if off, uses the serial data routine.
Then, in your sketch loop()
, you'd do something like:
graph
? displayGraph(depth, percent, litres)
: displayData(depth, percent, litres);
...or a bit more verbose rather than using the tertiary operator:
if (graph){
displayGraph(...);
}
else {
displayData(...);
}
Here's a Q&D serial plotter example of my displayGraph()
function above. In the top-right corner of the image, the four colours represent the four pieces of data I'm sending in, in order:
In my case, those represent the exponential moving average of the percentage of how full a liquid tank is, the second is the raw data I'm building that average on, third is the exponential moving average of how many litres are remaining in the tank, and lastly, again, the raw data for that average.
The sharp movements are simply me slapping the tank to get some variable and unstable readings for example purposes.
%
after it.