Categories

Espruino temperature display project

In this project we will display temperature from a DS18B20 on an OLED display.

Here is a picture of my project, you can see the temperature displayed on the OLED and the DS18B20 sesnor with an onboard power indicator LED

espruino-temp-project

LCD Wiring:

LCD pin Espruino
GND GND
VCC 3.3
SDA B7
SCL B6

DS18B20 Wiring:

DS18B20 module Espruino
GND GND
VCC 3.3
DQ (Data pin) B13

 

Layout

Here is a layout diagram of the project, the DS18B20 module I used is represented on the breadboard

espruino-and-oled-and-ds18b20_bb

 

Code

The code is fairly simple, you can change the pin that the DS18B20 connects to on the following line – var ow = new OneWire(B3);

There is also a temperature conversion from celsius to fahrenheit – var fahr = celsius * 1.8 + 32;

Another line worth looking at is the rounding of the temperature reading – celsius = Math.round(celsius*10)/10;

 

require("DS18B20");
require("SSD1306");
// I2C
I2C1.setup({scl:B6,sda:B7});
 
var ow = new OneWire(B3);
 
var g, temp;
 
function onInit() {
  clearInterval();
 
  temp = require("DS18B20").connect(ow);
  g = require("SSD1306").connect(I2C1, function() {
    setInterval(onTimer, 500);
  });
}
 
function onTimer() {
  // Get the temperature
  var celsius = temp.getTemp();
  var fahr = celsius * 1.8 + 32;
  // Round it to the nearest 0.1
  celsius = Math.round(celsius*10)/10;
  fahr = Math.round(fahr*10)/10;
  // display the celsius and fahr
  g.clear();
  g.setFontBitmap(); // simple 8x8 font
  g.drawString("Temperature",2,0);
  g.drawLine(0,10,84,10);
  g.setFontVector(20); // large font
  g.drawString(celsius+ " c", 0, 15);
  g.drawString(fahr+ " f", 0, 40);
  g.flip(); // copy this to the screen
};
 
onInit();

 

Links
MK00237 Small temperature sensor temperature measurement module DS18B20 module 18B20 module

128X64 0.96 Inch OLED LCD LED Display Module Yellow, blue double color and white e For Arduino 0.96″ IIC SPI

Espruino and DHT22 example

In this example we use the DHT22 (or AM2302) humidity/temperature sensor and the Arduino UNO board to read data and print it out to the serial monitor.

The DHT22 is better than the DHT11 because it has a wider range of measurement, 0 to 100% for humidity and -40°C to +125°C for temperature. Also it has a digital output that provides greater data accuracy.

am2302

The AM2302 is a wired version of the DHT22, in a large plastic body, so they are the same device. Connect the red 3.3V power, the yellow wire to your data input pin and the black wire to ground.

 

Schematic

Here are the pin connections

Device Pin – AM2302 colour Espruino
1 (Vcc) – Red wire 3.3
2 (S)  – Yellow wire B3
3 (GND) – Black wire GND

 

Here is a layout showing how to connect aDHT22

espruino-and-dht22_bb

Code

This example simply logs out temperature and humidity to the console every second

require("DHT22");
 
setInterval(function() {
 var dht = require("DHT22").connect(B3);
 dht.read(function (a){
 console.log("Temperature is "+a.temp.toString());
 console.log("Humidity is "+a.rh.toString());
 });
}, 1000);

 

Testing

You should see something like this in the console window

Temperature is 25.2
Humidity is 65.1
Temperature is 25.3
Humidity is 65.9
Temperature is 25.5
Humidity is 67.9
Temperature is 25.6
Humidity is 68.6
Temperature is 25.7
Humidity is 69.2
Temperature is 25.9
Humidity is 70.4
Temperature is 26
Humidity is 72.1
Temperature is 26.2
Humidity is 72.7

 

Link

AM2302 DHT22 temperature and humidity sensor for arduino

Microbit temperature in python

 

The BBC micro:bit does not have a dedicated temperature sensor. Instead, the temperature provided is actually the temperature of the silicon die on the main CPU.

So not a perfect measure of the temperature of a room but can be used a rough guide, you could also use this to monitor that your micro bit processor is not overheating

 

Code

from microbit import *
 
while True:
    sleep(5000)
    print(str(temperature())+" degrees C")

 

Testing

28 degrees C
28 degrees C
28 degrees C
28 degrees C
28 degrees C
28 degrees C
28 degrees C
28 degrees C
28 degrees C
28 degrees C

PIC16F877 and DS1820 temperature sensor example

In this example we connect the ever popular DS18B20 temperature sensor to our PIC, we will then display the temperature on our LCD

We used the DS18S20 in our example this was connected to PORT E pin 2 of our PIC16F877, once again we had a 16×2 LCD connected to PORT B.

Schematic

PIC16F877 and DS1820 example

PIC16F877 and DS1820 example

Code


sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

//  Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
//  18S20: 9  (default setting; can be 9,10,11,or 12)
//  18B20: 12
unsigned short TEMP_RESOLUTION = 9;
unsigned temp;
char *text = "000.00";
int i;
int colona;

void Read_Temperature()
{
// Perform temperature reading
Ow_Reset(&PORTE, 2);                         // Onewire reset signal
Ow_Write(&PORTE, 2, 0xCC);                   // Issue command SKIP_ROM
Ow_Write(&PORTE, 2, 0x44);                   // Issue command CONVERT_T
Delay_us(120);
Ow_Reset(&PORTE, 2);
Ow_Write(&PORTE, 2, 0xCC);                   // Issue command SKIP_ROM
Ow_Write(&PORTE, 2, 0xBE);                   // Issue command READ_SCRATCHPAD
temp =  Ow_Read(&PORTE, 2);
temp = (Ow_Read(&PORTE, 2) << 8) + temp;
}

void Display_Temperature()
{
unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole;
unsigned int temp2write;
unsigned int temp_fraction;

temp2write = temp;
// Check if temperature is negative
if (temp2write & 0x8000)
{
text[0] = '-';
temp2write = ~temp2write + 1;
}
// Extract temp_whole
temp_whole = temp2write >> RES_SHIFT ;
// Convert temp_whole to characters
if (temp_whole/100)
text[0] = temp_whole/100  + 48;

text[1] = (temp_whole/10)%10 + 48;             // Extract tens digit
text[2] =  temp_whole%10     + 48;             // Extract ones digit
text[3] = '.';
// Extract temp_fraction and convert it to unsigned int
temp_fraction  = temp2write << (4-RES_SHIFT);
temp_fraction &= 0x000F;
temp_fraction *= 625;
// Convert temp_fraction to characters
text[4] =  temp_fraction/1000    + 48;         // Extract thousands digit
// Print temperature on LCD
Lcd_Out(1, 1, text);
// Print degree character and'C' for Celsius
Lcd_Chr_CP(0xDF);                             // 223 ASCII for degree symbol on my LCD
Lcd_Chr_CP('C');
}

void main()
{
CMCON |=7;
ADCON1 = 0x0D;
TRISE.B2 = 1;                                  // Configure RE2 pin as input

Lcd_Init();                                    // Initialize LCD
Lcd_Cmd(_LCD_CLEAR);                           // Clear LCD
Lcd_Cmd(_LCD_CURSOR_OFF);                      // Turn cursor off

// Main loop
do
{
Read_Temperature();
Display_Temperature();
Delay_ms(100);
} while (1);
}

Links
DS1820 20pcs

DS1820 Temperature Module

PIC16F877 temperature and humidity example

In this example we will read the temperature and humidity using a DHT11 sensor and display the readings on our LCD. We have linked to the datasheet at the bottom of the page, download it and then you can read a bit more technical information on this sensor.

These sensors whether standalone or in the form of breakouts are relatively inexpensive and for most projects they have an acceptable level of accuracy

I tried a couple of examples on the internet and none of them worked for me. Fortunately these all seemed to use Port A.0, changing this to PORTD.0 fixed the problems for me.

Schematic

I’ve omitted the external crystal and power connections to the PIC micro, concentrated mainly on the connections between the DHT11, LCD and the PIC micro.

PIC16F877 and DHT11 schematic

PIC16F877 and DHT11 schematic

Code

mikroC for PIC code again


sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;


sbit Data at RD0_bit;
sbit DataDir at TRISD0_bit;
char message1[] = "Temp = 00.0 C";
char message2[] = "RH   = 00.0 %";
unsigned short TOUT = 0, CheckSum, i;
unsigned short T_Byte1, T_Byte2, RH_Byte1, RH_Byte2;

void StartSignal(){
DataDir = 0;     // Data port is output
Data    = 0;
delay_ms(18);
Data    = 1;
delay_us(30);
DataDir = 1;     // Data port is input

}

unsigned short CheckResponse(){
TOUT = 0;
TMR2 = 0;
T2CON.TMR2ON = 1;      // start timer
while(!Data && !TOUT);
if (TOUT) return 0;
else {
TMR2 = 0;
while(Data && !TOUT);
if (TOUT) return 0;
else {
T2CON.TMR2ON = 0;
return 1;
}
}
}

unsigned short ReadByte(){
unsigned short num = 0, t;
DataDir = 1;
for (i=0; i<8; i++){
while(!Data);
TMR2 = 0;
T2CON.TMR2ON = 1;
while(Data);
T2CON.TMR2ON = 0;
if(TMR2 > 40) num |= 1<<(7-i);  // If time > 40us, Data is 1
}
return num;
}

void interrupt(){
if(PIR1.TMR2IF){
TOUT = 1;
T2CON.TMR2ON = 0; // stop timer
PIR1.TMR2IF  = 0; // Clear TMR0 interrupt flag
}
}

void main() {
unsigned short check;
TRISB = 0b00000000;
PORTB = 0;
TRISD = 0b00000001;
CMCON = 7;
INTCON.GIE = 1;    //Enable global interrupt
INTCON.PEIE = 1;   //Enable peripheral interrupt
// Configure Timer2 module
PIE1.TMR2IE = 1;  // Enable Timer2 interrupt
T2CON = 0;        // Prescaler 1:1, and Timer2 is off initially
PIR1.TMR2IF =0;   // Clear TMR INT Flag bit
TMR2 = 0;
Lcd_Init();
Lcd_Cmd(_Lcd_Clear);
Lcd_Cmd(_LCD_CURSOR_OFF);

do {
Delay_ms(1000);
StartSignal();
check = CheckResponse();
if (!check) {
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(1, 1, "No response");
Lcd_Out(2, 1, "from the sensor");
}
else{

RH_Byte1 = ReadByte();
RH_Byte2 = ReadByte();
T_Byte1 = ReadByte();
T_Byte2 = ReadByte();
CheckSum = ReadByte();
// Check for error in Data reception
if (CheckSum == ((RH_Byte1 + RH_Byte2 + T_Byte1 + T_Byte2) & 0xFF))
{
message1[7]  = T_Byte1/10 + 48;
message1[8]  = T_Byte1%10 + 48;
message1[10] = T_Byte2/10 + 48;
message2[7]  = RH_Byte1/10 + 48;
message2[8]  = RH_Byte1%10 + 48;
message2[10] = RH_Byte2/10 + 48;
message1[11] = 223;     // Degree symbol
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(1, 1, message1);
Lcd_Out(2, 1, message2);
}

else{
Lcd_Cmd(_Lcd_Clear);
Lcd_Out(1, 1, "Checksum Error!");
Lcd_Out(2, 1, "Try Again.");
}
}

}while(1);
}


Links

DHT11 datasheet

1x DHT11 DHT-11 Digital Temperature and Humidity Temperature sensor