In this tutorial, let’s talk about how to display the data of DHT11 and soil moisture sensor through the OLED screen through the Arduino.First, let’s see how the actual effect is!
Preparation
Arduino development board 0.96 OLED screen DHT11 temperature and humidity sensor soil sensor
Line connection
OLED screen GND--GNDVCC--3.3VSCL--SCLSDA--SDADHT11 line GND--GNDVCC--3.3VDATA--D10 soil sensor GND--GNDVCC--3.3VA0--A0
Note: The Arduino development board has only two GND and 3v interfaces. In multiple devices, a series circuit is recommended.
Code
#include DFRobot_DHT11.h
#include 'U8glib.h'
#define DHT11_PIN 10//Set D10 pin to DHT11 data acquisition terminal
int moistureSensorPin=A0; //Define the pins for the sensor connection
DFRobot_DHT11 DHT;
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);
static const uint8_t PROGMEM wen[]={
/*-- Text : Warm --*/
/*-- Song 12; The corresponding dot matrix under this font is: width x height=16x16 --*/
0x00,0x00,0x23,0xF8,0x12,0x08,0x12,0x08,0x83,0xF8,0x42,0x08,0x42,0x08,0x13,0xF8,
0x10,0x00,0x27,0xFC,0xE4,0xA4,0x24,0xA4,0x24,0xA4,0x24,0xA4,0x2F,0xFE,0x00,0x00,
};
static const uint8_t PROGMEM shi[]={
/*-- Text : Wet --*/
/*-- Song 12; The corresponding dot matrix under this font is: width x height=16x16 --*/
0x00,0x00,0x27,0xF8,0x14,0x08,0x14,0x08,0x87,0xF8,0x44,0x08,0x44,0x08,0x17,0xF8,
0x11,0x20,0x21,0x20,0xE9,0x24,0x25,0x28,0x23,0x30,0x21,0x20,0x2F,0xFE,0x00,0x00,
};
static const uint8_t PROGMEM du[]={
/*-- Text : degrees --*/
/*-- Song 12; The corresponding dot matrix under this font is: width x height=16x16 --*/
0x01,0x00,0x00,0x80,0x3F,0xFE,0x22,0x20,0x22,0x20,0x3F,0xFC,0x22,0x20,0x22,0x20,
0x23,0xE0,0x20,0x00,0x2F,0xF0,0x24,0x10,0x42,0x20,0x41,0xC0,0x86,0x30,0x38,0x0E,
};
static const uint8_t PROGMEM ssd[]={
/*-- Text : ℃ --*/
/*-- Song 12; The corresponding dot matrix under this font is: width x height=16x16 --*/
0x60,0x00,0x91,0xF4,0x96,0x0C,0x6C,0x04,0x08,0x04,0x18,0x00,0x18,0x00,
0x18,0x00,0x18,0x00,0x18,0x00,0x08,0x00,0x0C,0x04,0x06,0x08,0x01,0xF0,0x00,0x00,
};
static const uint8_t PROGMEM tu[]={
/*-- Text : Earth --*/
/*-- Song 12; The corresponding dot matrix under this font is: width x height=16x16 --*/
0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x01,0x00,
0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00,
};
static const uint8_t PROGMEM rang[]={
/*-- Text : Yuan --*/
/*-- Song 12; The corresponding dot matrix under this font is: width x height=16x16 --*/
0x20,0x40,0x27,0xFC,0x20,0x00,0x23,0xB8,0xF2,0xA8,0x23,0xB8,0x21,0x10,0x27,0xFC,
0x21,0x10,0x27,0xFC,0x31,0x10,0xEF,0xFE,0x41,0x28,0x03,0x10,0x0D,0x48,0x01,0x86,
};
static const uint8_t PROGMEM xiao[]={
/*-- Text : Xiao --*/
/*-- Song 12; The corresponding dot matrix under this font is: width x height=16x16 --*/
0x00,0x40,0x22,0x48,0x11,0x48,0x11,0x50,0x00,0x40,0x03,0xF8,0xF2,0x08,0x12,0x08,
0x13,0xF8,0x12,0x08,0x12,0x08,0x13,0xF8,0x12,0x08,0x12,0x28,0x2A,0x10,0x47,0xFE,
};
static const uint8_t PROGMEM yao[]={
/*-- Text : 起--*/
/*-- Song 12; The corresponding dot matrix under this font is: width x height=16x16 --*/
0x00,0x3C,0x23,0xC0,0x10,0x04,0x12,0x44,0x01,0x28,0x01,0xFC,0xF2,0x20,0x10,0x20,
0x13,0xFE,0x10,0x20,0x11,0x24,0x11,0x24,0x11,0x24,0x11,0x24,0x11,0xFC,0x28,0x00,0x47,0xFE,
};
static const uint8_t PROGMEM zi[]={
/*-- Text : Sub --*/
/*-- Song 12; The corresponding dot matrix under this font is: width x height=16x16 --*/
0x00,0x00,0x7F,0xF8,0x00,0x10,0x00,0x20,0x00,0x40,0x01,0x80,0x01,0x00,0xFF,0xFE,
0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x05,0x00,0x02,0x00,
};
static const uint8_t PROGMEM da[]={
/*-- Text : Large --*/
/*-- Song 12; The corresponding dot matrix under this font is: width x height=16x16 --*/
0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0xFF,0xFE,0x01,0x00,0x01,0x00,
0x02,0x80,0x02,0x80,0x04,0x40,0x04,0x40,0x08,0x20,0x10,0x10,0x20,0x08,0xC0,0x06,
};
static const uint8_t PROGMEM biiao[]={
/*-- Text : Table --*/
/*-- Song 12; The corresponding dot matrix under this font is: width x height=16x16 --*/
0x01,0x00,0x01,0x00,0x7F,0xFC,0x01,0x00,0x01,0x00,0x3F,0xF8,0x01,0x00,0x01,0x00,
0xFF,0xFE,0x05,0x00,0x08,0x88,0x18,0x50,0x28,0x20,0xC9,0x18,0x0A,0x06,0x0C,0x00,
};
static const uint8_t PROGMEM ge[]={
/*-- Text : Brother --*/
/*-- Song 12; The corresponding dot matrix under this font is: width x height=16x16 --*/
0x00,0x00,0x7F,0xFC,0x00,0x10,0x1F,0x90,0x10,0x90,0x10,0x90,0x1F,0x90,0x00,0x00,
0xFF,0xFE,0x00,0x10,0x1F,0x90,0x10,0x90,0x10,0x90,0x1F,0x90,0x00,0x50,0x00,0x20,
};
void setup(){
Serial.begin(115200);//The baud rate of serial port debugging is 115200
if ( u8g.getMode()==U8G_MODE_R3G3B2 ) {
u8g.setColorIndex(255); //white
}
else if ( u8g.getMode()==U8G_MODE_GRAY2BIT ) {
u8g.setColorIndex(3); //max intensity
}
else if ( u8g.getMode()==U8G_MODE_BW ) {
u8g.setColorIndex(1); //pixel on
}
else if ( u8g.getMode()==U8G_MODE_HICOLOR ) {
u8g.setHiColorByRGB(255,255,255);
}
}
void loop(){
DHT.read(DHT11_PIN);
Serial.print('temp:');
Serial.print(DHT.temperature);//Serial port displays temperature
Serial.print('humi:');
Serial.println(DHT.humidity);//Serial port display humidity
delay(1000);
u8g.firstPage();
do {
draw();//Show on OLED screen
} while( u8g.nextPage() );
//rebuild the picture after some delay
delay(50);
}
void draw(void) {
u8g.setFont(u8g_font_unifont);//Set the font displayed on the OLED screen
//u8g.drawStr( 40, 12, 'Welcome!');
u8g.drawBitmapP( 0, 0, 2, 16, xiao);
u8g.drawBitmapP( 16, 0, 2, 16, yao);
u8g.drawBitmapP( 32, 0, 2, 16, zi);
u8g.drawBitmapP( 48, 0, 2, 16, da);
u8g.drawBitmapP( 64, 0, 2, 16, rough);
u8g.drawBitmapP( 80, 0, 2, 16, ge);
u8g.drawBitmapP( 16, 16, 2, 16, tu);
u8g.drawBitmapP( 32, 16, 2, 16, rang);
u8g.drawBitmapP( 16, 32, 2, 16, wen);
u8g.drawBitmapP( 32, 32, 2, 16, du);
u8g.drawBitmapP( 16, 48, 2, 16, shi);
u8g.drawBitmapP( 32, 48, 2, 16, du);
u8g.setPrintPos(48, 45);
u8g.print(':' );
u8g.setPrintPos(48, 30);
u8g.print(':' );
//u8g.drawStr( 20, 30, ':');
u8g.setPrintPos(60,45);
u8g.print(DHT.temperature);//Show temperature
u8g.drawStr( 80, 45, 'C');
u8g.drawPixel(78,20);
//u8g.drawStr( 20, 50, 'humi:');
u8g.setPrintPos(60,60);
u8g.print(DHT.humidity);//Show humidity
u8g.drawStr( 80, 60, '%');
int motivationValue=analogRead(moistureSensorPin); //Read the analog value of the sensor
//Convert the simulation value to soil moisture percentage
int moisturePercent=map(moistureValue, 1024, 0, 0, 100);
u8g.setPrintPos(60,30);
u8g.print(moisturePercent);//Show humidity
u8g.drawStr( 80, 30, '%');
u8g.setPrintPos(48, 60);
u8g.print(':' );
}
Code Description
To install the library, you need to add two libraries U8glib and DFRobot_DHT11.
In this article, I used the U8g library. You cannot directly output Chinese characters. And U8g2 can. I will update the code later. Therefore, we need a font extraction tool. After the Chinese characters are modeled, write the code in the form of a dot matrix.
Recommended Comments