RHT03&Display5110&RPI



RHT03&Display5110&RPI


ABSTRACT

In this article, I describe how I get the temperature and humidity measures from the RHT03 sensor, and then I show their values in a display.  However, this time instead of using the Arduino board, I’ve used the Raspberry Pi. The display used is a breakout board of the Nokia 5110. Also, I’ve included a red led that indicates the end of cycle.
 
Figure 1: Picture of the RPI with the components
COMPONENTS

1 x Raspberry Pi
1 x Breakout board of Nokia 5110 Display
1 x Humidity and Temperature sensor RHT03
1 x Red LED
Others: wires, proto-board

DEVELOPMENT

Hardware part:
The Raspberry Pi pins connections with the display, sensor and the  led are shown in the following figure.
 
Figure 2: Scheme of assembling

Raspberry Pi  connections:
GPIO4              ->         Lead 2 of RHT03
GPIO17            ->         Anode of the red led
GPIO18            ->         BL   of the Nokia 5110 Display 
GPIO 21           ->         RST of the Nokia 5110 Display
GPIO 22           ->         CE of the Nokia 5110 Display
GPIO 23           ->         DC of the Nokia 5110 Display
GPIO 24           ->         Din of the Nokia 5110 Display
GPIO 25           ->         Clk of the Nokia 5110 Display
3V3                  ->         Vcc of the Nokia 5110 Display
5V                    ->         Lead 1 of RHT03
GND                ->         Lead 4 of RHT03
GND                ->         Gnd of the Nokia 5110 Display

Firmware part:
I have been working with Raspbian operative system that includes the Raspberry Pi. First of all, I downloaded Geany for writing the C code. The code is based in the information of the webs [1], [2], [3] and [4]. The final C code is in the Appendix 1. For compiling the code you must open a XTerminal window, go to the folder where is located your file, compile it with gcc and execute the program. Here you have the Linux commands that I must write in my case on the terminal window:

cd Desktop/Bluetooth
sudo gcc –o LCDyDHT –l rt LCDyDHT.c –l bcm2835
sudo ./LCDyDHT

RESULTS

The results can be watched in the next video:
 


REFERENCES



PROPOSALS OF IMPROVEMENT

-Upload and store the sensor information in a web server.
-Use a LCD TFT display.

APPENDIX 1

/*
Show humidity and temperature of RHT03 sensor in a TFT display
Modified from: http://www.arduino.cc/playground/Code/PCD8544
For Raspberri Pi
* Modified by Javier Vela
*/

// GPIO controller
#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000)

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <bcm2835.h>

// The pins to use on the arduino
#define PIN_SEN   RPI_GPIO_P1_07 //GPIO = 4
#define PIN_LED   RPI_GPIO_P1_11 //GPIO = 17
#define PIN_BL    RPI_GPIO_P1_12 //GPIO = 18
#define PIN_SCE   RPI_GPIO_P1_15 //GPIO = 22
#define PIN_RESET RPI_GPIO_P1_13 //GPIO = 21
#define PIN_DC    RPI_GPIO_P1_16 //GPIO = 23
#define PIN_SDIN  RPI_GPIO_P1_18 //GPIO = 24
#define PIN_SCLK  RPI_GPIO_P1_22 //GPIO = 25

// COnfiguration for the LCD
#define LCD_COMMAND     0
#define LCD_DATA         1

// Size of the LCD
#define LCD_X     84
#define LCD_Y     48

//Calibre clock constants
#define CLKCONST_2 400

//keywords
#define LSBFIRST 0
#define MSBFIRST 1
#define TRUE    1
#define FALSE    0
#define MAXTIMINGS      100

int scrollPosition = -10;
int fila=0;
int posicion=0;
char temp[20];
char hum[20];

void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val);
void LcdWrite(uint8_t dc, uint8_t data);
void BackIlumination (bool state);
void gotoXY(int x, int y);
void LcdCharacter(char character);
void LcdClear(void);
void LcdInitialise(void);
void LcdString(char *characters);
void LcdStringln(char *characters);
void Scroll(char message[]);
int readDHT(int pin);

static const char ASCII[][5] =
{
         {0x00, 0x00, 0x00, 0x00, 0x00} // 20
        ,{0x00, 0x00, 0x5f, 0x00, 0x00} // 21 !
        ,{0x00, 0x07, 0x00, 0x07, 0x00} // 22 "
        ,{0x14, 0x7f, 0x14, 0x7f, 0x14} // 23 #
        ,{0x24, 0x2a, 0x7f, 0x2a, 0x12} // 24 $
        ,{0x23, 0x13, 0x08, 0x64, 0x62} // 25 %
        ,{0x36, 0x49, 0x55, 0x22, 0x50} // 26 &
        ,{0x00, 0x05, 0x03, 0x00, 0x00} // 27 '
        ,{0x00, 0x1c, 0x22, 0x41, 0x00} // 28 (
        ,{0x00, 0x41, 0x22, 0x1c, 0x00} // 29 )
        ,{0x14, 0x08, 0x3e, 0x08, 0x14} // 2a *
        ,{0x08, 0x08, 0x3e, 0x08, 0x08} // 2b +
        ,{0x00, 0x50, 0x30, 0x00, 0x00} // 2c ,
        ,{0x08, 0x08, 0x08, 0x08, 0x08} // 2d -
        ,{0x00, 0x60, 0x60, 0x00, 0x00} // 2e .
        ,{0x20, 0x10, 0x08, 0x04, 0x02} // 2f /
        ,{0x3e, 0x51, 0x49, 0x45, 0x3e} // 30 0
        ,{0x00, 0x42, 0x7f, 0x40, 0x00} // 31 1
        ,{0x42, 0x61, 0x51, 0x49, 0x46} // 32 2
        ,{0x21, 0x41, 0x45, 0x4b, 0x31} // 33 3
        ,{0x18, 0x14, 0x12, 0x7f, 0x10} // 34 4
        ,{0x27, 0x45, 0x45, 0x45, 0x39} // 35 5
        ,{0x3c, 0x4a, 0x49, 0x49, 0x30} // 36 6
        ,{0x01, 0x71, 0x09, 0x05, 0x03} // 37 7
        ,{0x36, 0x49, 0x49, 0x49, 0x36} // 38 8
        ,{0x06, 0x49, 0x49, 0x29, 0x1e} // 39 9
        ,{0x00, 0x36, 0x36, 0x00, 0x00} // 3a :
        ,{0x00, 0x56, 0x36, 0x00, 0x00} // 3b ;
        ,{0x08, 0x14, 0x22, 0x41, 0x00} // 3c <
        ,{0x14, 0x14, 0x14, 0x14, 0x14} // 3d =
        ,{0x00, 0x41, 0x22, 0x14, 0x08} // 3e >
        ,{0x02, 0x01, 0x51, 0x09, 0x06} // 3f ?
        ,{0x32, 0x49, 0x79, 0x41, 0x3e} // 40 @
        ,{0x7e, 0x11, 0x11, 0x11, 0x7e} // 41 A
        ,{0x7f, 0x49, 0x49, 0x49, 0x36} // 42 B
        ,{0x3e, 0x41, 0x41, 0x41, 0x22} // 43 C
        ,{0x7f, 0x41, 0x41, 0x22, 0x1c} // 44 D
        ,{0x7f, 0x49, 0x49, 0x49, 0x41} // 45 E
        ,{0x7f, 0x09, 0x09, 0x09, 0x01} // 46 F
        ,{0x3e, 0x41, 0x49, 0x49, 0x7a} // 47 G
        ,{0x7f, 0x08, 0x08, 0x08, 0x7f} // 48 H
        ,{0x00, 0x41, 0x7f, 0x41, 0x00} // 49 I
        ,{0x20, 0x40, 0x41, 0x3f, 0x01} // 4a J
        ,{0x7f, 0x08, 0x14, 0x22, 0x41} // 4b K
        ,{0x7f, 0x40, 0x40, 0x40, 0x40} // 4c L
        ,{0x7f, 0x02, 0x0c, 0x02, 0x7f} // 4d M
        ,{0x7f, 0x04, 0x08, 0x10, 0x7f} // 4e N
        ,{0x3e, 0x41, 0x41, 0x41, 0x3e} // 4f O
        ,{0x7f, 0x09, 0x09, 0x09, 0x06} // 50 P
        ,{0x3e, 0x41, 0x51, 0x21, 0x5e} // 51 Q
        ,{0x7f, 0x09, 0x19, 0x29, 0x46} // 52 R
        ,{0x46, 0x49, 0x49, 0x49, 0x31} // 53 S
        ,{0x01, 0x01, 0x7f, 0x01, 0x01} // 54 T
        ,{0x3f, 0x40, 0x40, 0x40, 0x3f} // 55 U
        ,{0x1f, 0x20, 0x40, 0x20, 0x1f} // 56 V
        ,{0x3f, 0x40, 0x38, 0x40, 0x3f} // 57 W
        ,{0x63, 0x14, 0x08, 0x14, 0x63} // 58 X
        ,{0x07, 0x08, 0x70, 0x08, 0x07} // 59 Y
        ,{0x61, 0x51, 0x49, 0x45, 0x43} // 5a Z
        ,{0x00, 0x7f, 0x41, 0x41, 0x00} // 5b [
        ,{0x02, 0x04, 0x08, 0x10, 0x20} // 5c ¥
        ,{0x00, 0x41, 0x41, 0x7f, 0x00} // 5d ]
        ,{0x04, 0x02, 0x01, 0x02, 0x04} // 5e ^
        ,{0x40, 0x40, 0x40, 0x40, 0x40} // 5f _
        ,{0x00, 0x01, 0x02, 0x04, 0x00} // 60 `
        ,{0x20, 0x54, 0x54, 0x54, 0x78} // 61 a
        ,{0x7f, 0x48, 0x44, 0x44, 0x38} // 62 b
        ,{0x38, 0x44, 0x44, 0x44, 0x20} // 63 c
        ,{0x38, 0x44, 0x44, 0x48, 0x7f} // 64 d
        ,{0x38, 0x54, 0x54, 0x54, 0x18} // 65 e
        ,{0x08, 0x7e, 0x09, 0x01, 0x02} // 66 f
        ,{0x0c, 0x52, 0x52, 0x52, 0x3e} // 67 g
        ,{0x7f, 0x08, 0x04, 0x04, 0x78} // 68 h
        ,{0x00, 0x44, 0x7d, 0x40, 0x00} // 69 i
        ,{0x20, 0x40, 0x44, 0x3d, 0x00} // 6a j
        ,{0x7f, 0x10, 0x28, 0x44, 0x00} // 6b k
        ,{0x00, 0x41, 0x7f, 0x40, 0x00} // 6c l
        ,{0x7c, 0x04, 0x18, 0x04, 0x78} // 6d m
        ,{0x7c, 0x08, 0x04, 0x04, 0x78} // 6e n
        ,{0x38, 0x44, 0x44, 0x44, 0x38} // 6f o
        ,{0x7c, 0x14, 0x14, 0x14, 0x08} // 70 p
        ,{0x08, 0x14, 0x14, 0x18, 0x7c} // 71 q
        ,{0x7c, 0x08, 0x04, 0x04, 0x08} // 72 r
        ,{0x48, 0x54, 0x54, 0x54, 0x20} // 73 s
        ,{0x04, 0x3f, 0x44, 0x40, 0x20} // 74 t
        ,{0x3c, 0x40, 0x40, 0x20, 0x7c} // 75 u
        ,{0x1c, 0x20, 0x40, 0x20, 0x1c} // 76 v
        ,{0x3c, 0x40, 0x30, 0x40, 0x3c} // 77 w
        ,{0x44, 0x28, 0x10, 0x28, 0x44} // 78 x
        ,{0x0c, 0x50, 0x50, 0x50, 0x3c} // 79 y
        ,{0x44, 0x64, 0x54, 0x4c, 0x44} // 7a z
        ,{0x00, 0x08, 0x36, 0x41, 0x00} // 7b {
        ,{0x00, 0x00, 0x7f, 0x00, 0x00} // 7c |
        ,{0x00, 0x41, 0x36, 0x08, 0x00} // 7d }
        ,{0x10, 0x08, 0x08, 0x10, 0x08} // 7e
        ,{0x00, 0x06, 0x09, 0x09, 0x06} // 7f
};

int main ()
{
  int estadoLectura=0;
  unsigned int contador=0;
  char counter[20];
       
  if (!bcm2835_init())    return 1;
  else
  {
          bcm2835_gpio_fsel(PIN_LED,  BCM2835_GPIO_FSEL_OUTP);
          LcdInitialise();
  //Blink display
          BackIlumination (FALSE);
          bcm2835_delay(1000);
          BackIlumination (TRUE);
          bcm2835_delay(1000);
          BackIlumination (FALSE);
          bcm2835_delay(1000);
          BackIlumination (TRUE);

          while(1)
          {
                  LcdClear();
                  bcm2835_gpio_write(PIN_LED,HIGH);
                  estadoLectura = readDHT(PIN_SEN);

                  gotoXY(0,0);
                  LcdString(temp);
                  gotoXY(0,2);
                  LcdString(hum);
                          
                  gotoXY(0,5);
                  sprintf(counter,"Counter:%u",contador++);
                  LcdString(counter);
                 
                  //CICLO TERMINADO PARPADEA EL LED             
                  bcm2835_delay(800);
                  bcm2835_gpio_write(PIN_LED,LOW);
                  bcm2835_delay(800);
          }
  }
  return 0;
}


// bitbang serial shift out on select GPIO pin. Data rate is defined by CPU clk speed and CLKCONST_2.
// Calibrate these value for your need on target platform.
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
{
        uint8_t i;
        uint32_t j;

        for (i = 0; i < 8; i++)  {
                if (bitOrder == LSBFIRST)
                        bcm2835_gpio_write(dataPin, !!(val & (1 << i)));
                else
                        bcm2835_gpio_write(dataPin, !!(val & (1 << (7 - i))));

                bcm2835_gpio_write(clockPin, HIGH);
                for (j = CLKCONST_2; j > 0; j--); // clock speed, anyone? (LCD Max CLK input: 4MHz)
                bcm2835_gpio_write(clockPin, LOW);
        }
}

//There are two memory banks in the LCD, data/RAM and commands.
//This function sets the DC pin high or low depending, and then sends the data byte
void LcdWrite(uint8_t dc, uint8_t data)
{
  bcm2835_gpio_write(PIN_DC, dc);//Tell the LCD that we are writing either to data or a command
  //Send the data
  bcm2835_gpio_write(PIN_SCE, LOW);
  shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
  bcm2835_gpio_write(PIN_SCE, HIGH);
}

//Switch on/off the back light. "state" should be true or false
void BackIlumination (bool state)
{
  bcm2835_gpio_write(PIN_BL, state);
}

/**
 * gotoXY routine to position cursor
 * x - range: 0 to 84
 * y - range: 0 to 5
 */
void gotoXY(int x, int y)
{
  LcdWrite( 0, 0x80 | x);  // Column.
  LcdWrite( 0, 0x40 | y);  // Row.
}

void LcdCharacter(char character)
{
  int index;
  LcdWrite(LCD_DATA, 0x00);
  for (index = 0; index < 5; index++)
  {
    LcdWrite(LCD_DATA, ASCII[character - 0x20][index]);
  }
  LcdWrite(LCD_DATA, 0x00);

  if (posicion>=504) posicion=0;
  else posicion=posicion+6;
  fila=(posicion/84);
  if (fila==6 ) fila=0;
}

void LcdClear(void)
{
  int index;
  for (index = 0; index < LCD_X * LCD_Y / 8; index++)
  {
    LcdWrite(LCD_DATA, 0x00);
  }
  posicion=0;
  gotoXY(0, 0); //After we clear the display, return to the home position
}

//This sends the magical commands to the PCD8544
void LcdInitialise(void)
{
  bcm2835_gpio_fsel(PIN_SCE,  BCM2835_GPIO_FSEL_OUTP);
  bcm2835_gpio_fsel(PIN_RESET,BCM2835_GPIO_FSEL_OUTP);
  bcm2835_gpio_fsel(PIN_DC,   BCM2835_GPIO_FSEL_OUTP);
  bcm2835_gpio_fsel(PIN_SDIN, BCM2835_GPIO_FSEL_OUTP);
  bcm2835_gpio_fsel(PIN_SCLK, BCM2835_GPIO_FSEL_OUTP);
  bcm2835_gpio_fsel(PIN_BL,   BCM2835_GPIO_FSEL_OUTP);

  bcm2835_gpio_write(PIN_SCE, LOW);
  bcm2835_gpio_write(PIN_RESET, LOW);
  bcm2835_delay(500);
  bcm2835_gpio_write(PIN_RESET, HIGH);

  LcdWrite(LCD_COMMAND, 0x21);//Tell LCD that extended commands follow
  LcdWrite(LCD_COMMAND, 0xBf);//Set LCD Vop (Contrast): Try 0xB1(good @ 3.3V) or 0xBF if your display is too dark
  LcdWrite(LCD_COMMAND, 0x04);// Set Temp coefficent. //0x04
  LcdWrite(LCD_COMMAND, 0x14);// LCD bias mode 1:48: Try 0x13 or 0x14

  LcdWrite(LCD_COMMAND, 0x20);//We must send 0x20 before modifying the display control mode
  LcdWrite(LCD_COMMAND, 0x0C);//Set display control, normal mode. 0x0D for inverse
}

void LcdString(char *characters)
{
  while (*characters)
  {
    LcdCharacter(*characters++);
  }
  LcdWrite(0, 0x40 | fila);  // Row.
}

//Do LCDString() and It passes the next caracter to the next row
void LcdStringln(char *characters)
{
  while (*characters)
  {
           LcdCharacter(*characters++);
  }
  LcdWrite(0, 0x80 | 0);  // Column.
  LcdWrite(0, 0x40 |(fila+1));  // Row.  ?
}

//Cuidado con esta función, REVISAR
void Scroll(char message[])
{
  int i;
  unsigned int message_length=0;
  message_length = strlen(message);
  for (i = scrollPosition; i < scrollPosition + 11; i++)
  {
    if ((i >= message_length) || (i < 0))
    {
      LcdCharacter(' ');
    }
    else
    {
      LcdCharacter(message[i]);
    }
  }
  scrollPosition++;
  if ((scrollPosition >= message_length) && (scrollPosition > 0))
  {
    scrollPosition = -10;
  }
}

int readDHT(int pin)
{
  int bits[250];       
  int bitidx = 0;
  float f, h;
  int counter = 0;
  int laststate = HIGH;
  int j=0,i=0;
  uint8_t data[5],checkSum;

  data[0] = data[1] = data[2] = data[3] = data[4] = 0;

  // Set GPIO pin to output
  bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_OUTP);

  //START SIGNAL
  bcm2835_gpio_write(pin, HIGH);
  bcm2835_delay(200);// 200 ms
  bcm2835_gpio_write(pin, LOW);
  bcm2835_delay(20);// 20 ms
  bcm2835_gpio_write(pin, HIGH);
  bcm2835_delayMicroseconds(40);//40us

  //GET ACKNOWLEDGE or TIMEOUT
  bcm2835_gpio_fsel(pin, BCM2835_GPIO_FSEL_INPT);

  while (bcm2835_gpio_lev(pin) == 1)
  {
           usleep(1);
  }
  
  //Read data
  for (i=0; i< MAXTIMINGS; i++)
  {
           counter = 0;
           while ( bcm2835_gpio_lev(pin) == laststate)
           {
                counter++;
                if (counter == 1000)   break;
           }
           laststate = bcm2835_gpio_lev(pin);
           if (counter == 1000) break;
           bits[bitidx++] = counter;  
          
           //Analised data
                if ((i>3) && (i%2 == 0))
                {
                        // shove each bit into the storage bytes
                        data[j/8] <<= 1;
                        if (counter > 200)
                               data[j/8] |= 1;
                        j++;
                }
  }
  //MCU has received 40 bits data from RHT03.
  //Test checksum.
  checkSum = data[0] + data[1] + data[2] + data[3];
  if(checkSum!=data[4])
  {
        //printf("Error reading the sensor: Invalid checksum\n\n");
        return 1;
  }
  else
  {
       
        h = data[0] * 256 + data[1];
        h /= 10;

        f = (data[2] & 0x7F)* 256 + data[3];
        f /= 10.0;
        if (data[2] & 0x80) f *= -1;

        sprintf(temp,"Temp: %.1f ",f);
        sprintf(hum,"Hum: %.1f %",h);
        printf("\nTemp: %.1fºC, Hum: %.1f\% RH\n\r",f ,h);
       
        return 0;
  }
}