The Start

Hi, What and Why

Plug it in

Rule number 1

Water Sensor

Sound Sensor


Tri Colour LED

RTC (Real Time Clock) DS1302

RTC (Real Time Clock) DS3231

Matrix LED step 1


Stepper Motor

LCD revisited with PCF8574T

Humidity Sensor

Shift Register

RFID tags (RC-522)

7 Segment display

Ultrasonic distance sensor

5V regulator

analogRead and analogWrite

Wiring an Array of Switches

The next step

Other things I have bought

Infra red and Processing

Programming a separate arduino chip

Creating your own PCB

L293D for a DC motor

4 digit 7 segment display

Starting with motors

RF433 Wireless Comms

Sort a character array

More stuff

I2C devices (SDA,SCL)

I2C scanner

SPI devices (MOSI,MISO)

HMC5883L Compass

MMA7361 Accelerometer

Added projects

Message Display System

4WD robot car
4WD robot car II

4WD robot car COMPLETE

MP3 Player

Humidity Sensor - melted

A humidity sensor. This is after it melted; I didn't take a photo beforehand.

According to what I read, reading the humidity sensor is more complicated than reading a pin voltage.
So I plugged it in and ran the sketch - and yes, it melted. I didn't think that would be possible using only 5V, neither did I anticipate someone putting tutorial code online that would cause damage.
I have no idea whether the code causes intentional damage, or whether I plugged the sensor in incorrectly. I didn't care that much, but used more caution from henceforth.
Humidity sensor

The code is below; either avoid it, or for more experienced people, feel free to have a look and work out how it causes damage. I simply put it to one side and moved on.

Another pic of a melted humidity sensor.
Humidity sensor

This is the code that I used, which melted this sensor. Obviously, do not use this code unless you wish to see if you can melt yours as well.


//vers 08 Mar 13, 09:36 (Dave had an old "version" ID here (TKB edit))

//Changes added by DaveB 5 March 2013 as a result of Thomas Henry's
//article on the DHT22 in the March 2013 issue of Nuts&Volts.
//Thanks to
//for providing the original code to work from.

//This is a re-written DHT11/ DHT22 reading code.
//DHT stuff in subroutines.

//See for more information....

//N.B. "bit" is used in the narrow, computer "1 or 0"
//   sense throughout.

//"DHT" from sensor's names: DHT11, DHT22.
//DHT aka Aosong AM2302, and there's an AM2303 which
//seems to be in the same family.

//Comments on this based on Aosong AM2302, aka DHT22, datasheet.
//Believed to generally apply to DHT11 as well, except in the
//case of the DHT11, I believe the second and fourth bytes are
//always zero.

//The code WORKS... the comments may not yet be EXACTLY right.
//See the web-page cited above for latest news.

//This code works with a DHT11 humidity/ temperature sensing module
//from, complied with vers 0018 of the Arduino environment
//Sensor attached to P4 (nuelectronics shield)/ analog 0, aka digital 14.

//That "module", according to the
//nuelectronics site, and visual inspection simply provides for easy
//connection of an Aosong DHT11 unit to the nuelectronics datalogging
//shield. Only 3 wires are involved: Vcc, ground, and a single data
//line. One of the DHT11's 4 pins goes nowhere.

//You should not need to change anything except the next line to use
//the software with the sensor on a different line, or for a DHT22.

//Just "huffing" on the sensor from deeply filled lungs should show
//a near instant rise in humidity

//#define dht_PIN 0      //no ; here. deprecate ADC0...
  //even though we are using it as a digital pin.
  //Other parts of code restrict us to using
  //ADC0-5, aka D14-19
#define dht_dpin 14 //no ; here. Set equal to channel sensor is on,
     //where if dht_dpin is 14, sensor is on digital line 14, aka analog 0

byte bGlobalErr;//for passing error code back from complex functions.
byte dht_dat[5];//Array to hold the bytes sent from sensor.

float lastHumidity;        //Added by DaveB
float lastTemperatureF;        //Added by DaveB
float lastTemperatureC;        //Added by DaveB

const byte mask = 0x7f;        //Added by DaveB
const int ledPin =  8;      // the number of the LED pin, added by DaveB

int ledState = LOW;             // ledState used to set the LED, added by DaveB

void setup(){
InitDHT();//Do what's necessary to prepare for reading DHT
delay(1000);  //Let system settle, changed from 300 to 1000 by DaveB to allow
              //2 sec delay before addressing DHT22 again.
Serial.println("Humidity and temperature\n\n");
delay(1000);  //Wait rest of 1000ms recommended delay before accessing sensor,
              // changed from 700 to 1000 for a total of 2000 by DaveB

// set the digital pin as output to use the Arduino LED, Added by DaveB
pinMode(ledPin, OUTPUT);        //Added by DaveB

}//end "setup()"

void loop(){
  ReadDHT();//This is the "heart" of the program.
      //Fills global array dht_dpin[], and bGlobalErr, which
      //will hold zero if ReadDHT went okay.
      //Must call InitDHT once (in "setup()" is usual) before
      //calling ReadDHT.
  //Following: Display what was seen...
  switch (bGlobalErr){
     case 0:    //case 0 changed by DaveB

        //Changes added by DaveB as a result of Thomas Henry's article on the DHT22
        //in the March 2013 issue of Nuts&Volts.
        //Values received from the DHT22 are in two bytes that represent 10 times the value.
        //Therefore 256 * first word plus second word provides 10 times the humidity or
        //temperature value.  MSB of dht_dat[2] tells us if the temperature is
        //positive or negative.  MSB value of 0 is pos temp, MSB value of 1 is a neg temp.

        lastHumidity = (dht_dat[0]*256+dht_dat[1]);  //Calculate float for 10*humidity
        lastHumidity = lastHumidity/10;  //Calculate humidity
	Serial.print("Humidity: ");
        Serial.println("%  ");

        //dht_dat[2] = dht_dat[2] + 128;  // debug for showing negative temperature, uncomment to show neg temps
        // check for MSB set in dht_dat[2] which means negative temperature
        if ((dht_dat[2] & mask) != dht_dat[2]) {   //if they're different, it's negative
           dht_dat[2]= dht_dat[2] & mask;
           lastTemperatureC = -1*(dht_dat[2]*256+dht_dat[3]);  //Calculate neg float 10*temperature C
        else {   //if they're not different, it's positive
           lastTemperatureC = (dht_dat[2]*256+dht_dat[3]);  //Calculate float 10*temperature C

	lastTemperatureC = lastTemperatureC/10;  //Calculate temperature C
        Serial.print("Temp: ");
        Serial.print(lastTemperatureC);  //Uncomment these two lines for C temps
        Serial.print("C, ");             //Uncomment these two lines for C temps
        lastTemperatureF=lastTemperatureC*9/5+32;  //Calculate temperature F
        Serial.println("F  ");

         if (ledState == LOW)// alternately light and extinguish the LED
            ledState = HIGH;
            ledState = LOW;
         digitalWrite(ledPin, ledState);

     case 1:
        Serial.println("Error 1: DHT start condition 1 not met.");
     case 2:
        Serial.println("Error 2: DHT start condition 2 not met.");
     case 3:
        Serial.println("Error 3: DHT checksum error.");
        Serial.println("Error: Unrecognized code encountered.");
     	}//end "switch"

  //delay changed from 800 to 2000 by DaveB
  delay(2000);//Don't try to access too frequently... in theory
                   //should be once per two seconds, fastest,
                   //but seems to work after 0.8 second.
}// end loop()

/*Below here: Only "black box" elements which can just be plugged unchanged
  unchanged into programs. Provide InitDHT() and ReadDHT(), and a function
  one of them uses.*/

void InitDHT(){
  	//DDRC |= _BV(dht_PIN);//set data pin... for now... as output
              //DDRC is data direction register for pins A0-5 are on
	//PORTC |= _BV(dht_PIN);//Set line high
              //PORTC relates to the pins A0-5 are on.
        //Alternative code...
//        if (dht_dpin-14 != dht_PIN){Serial.println("ERROR- dht_dpin must be 14 more than dht_PIN");};//end InitDHT
        pinMode(dht_dpin,OUTPUT);// replaces DDRC... as long as dht_dpin=14->19
        digitalWrite(dht_dpin,HIGH);//Replaces PORTC |= if dht_pin=14->19
}//end InitDHT

void ReadDHT(){
/*Uses global variables dht_dat[0-4], and bGlobalErr to pass
  "answer" back. bGlobalErr=0 if read went okay.
  Depends on global dht_PIN for where to look for sensor.*/
byte dht_in;
byte i;
  // Send "start read and report" command to sensor....
  // First: pull-down i/o pin for 18ms
digitalWrite(dht_dpin,LOW);//Was: PORTC &= ~_BV(dht_PIN);
//db deleted following line
//delay(5);//TKB, from Quine at Arduino forum
/* datasheet for DHT22 says pin should be low at least
  500us. I infer it can be low longer without any]
  penalty apart from making "read sensor" process take
  longer. */
//Next line: Brings line high again,
//   second step in giving "start read..." command
digitalWrite(dht_dpin,HIGH);//Was: PORTC |= _BV(dht_PIN);
delayMicroseconds(30);//DHT22 datasheet says host should
   //keep line high 20-40us, then watch for sensor taking line
   //low. That low should last 80us. Acknowledges "start read
   //and report" command.
   //Getting the delay exacly right is critical...
   //  put 40 in for the delay, and the program
   //  will work SOMETIMES, but not ALWAYS.
   //  You have to allow for processing overheads,
   //  etc, too. Using, say, 30 won't "magically"
   //  make the TOTAL delay (with overheads, etc)
   //  exactly 30.0000 milliseconds.

//Next: Change Arduino pin to an input, to
//watch for the 80us low explained a moment ago.
pinMode(dht_dpin,INPUT);//Was: DDRC &= ~_BV(dht_PIN);


//Read the LO
dht_in=digitalRead(dht_dpin);//Was: dht_in = PINC & _BV(dht_PIN);

   bGlobalErr=1;//Was: Serial.println("dht11 start condition 1 not met");
   }//end "if..."

dht_in=digitalRead(dht_dpin);//Was: dht_in = PINC & _BV(dht_PIN);

   bGlobalErr=2;//Was: Serial.println("dht11 start condition 2 not met");
   }//end "if..."

/*After 80us low, the line should be taken high for 80us by the
  sensor. The low following that high is the start of the first
  bit of the forty to come. The routine "read_dht_dat()"
  expects to be called with the system already into this low.*/
//now ready for data reception... pick up the 5 bytes coming from
//   the sensor
for (i=0; i<5; i++)
   dht_dat[i] = read_dht_dat();

//Next: restore pin to output duties
pinMode(dht_dpin,OUTPUT);//Was: DDRC |= _BV(dht_PIN);
//N.B.: Using DDRC put restrictions on value of dht_pin

//Next: Make data line high again, as output from Arduino
digitalWrite(dht_dpin,HIGH);//Was: PORTC |= _BV(dht_PIN);
//N.B.: Using PORTC put restrictions on value of dht_pin

//Next see if data received consistent with checksum received
byte dht_check_sum =
/*Condition in following "if" says "if fifth byte from sensor
       not the same as the sum of the first four..."*/
if(dht_dat[4]!= dht_check_sum)
   {bGlobalErr=3;}//Was: Serial.println("DHT11 checksum error");
};//end ReadDHT()

byte read_dht_dat(){
//Collect 8 bits from datastream, return them interpreted
//as a byte. I.e. if 0000.0101 is sent, return decimal 5.

//Code expects the system to have recently entered the
//dataline low condition at the start of every data bit's
//transmission BEFORE this function is called.

  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++){
      //We enter this during the first start bit (low for 50uS) of the byte
      //Next: wait until pin goes high
      while(digitalRead(dht_dpin)==LOW);//Was: while(!(PINC & _BV(dht_PIN)));
            //signalling end of start of bit's transmission.

      //Dataline will now stay high for 27 or 70 uS, depending on
            //whether a 0 or a 1 is being sent, respectively.
      delayMicroseconds(30);//AFTER pin is high, wait further period, to be
        //into the part of the timing diagram where a 0 or a 1 denotes
        //the datum being send. The "further period" was 30uS in the software
        //that this has been created from. I believe that a higher number
        //(45?) would be more appropriate.

      //Next: Wait while pin still high
      if (digitalRead(dht_dpin)==HIGH)//Was: if(PINC & _BV(dht_PIN))
 	   result |=(1<<(7-i));// "add" (not just addition) the 1
                      //to the growing byte
    //Next wait until pin goes low again, which signals the START
    //of the NEXT bit's transmission.
    while (digitalRead(dht_dpin)==HIGH);//Was: while((PINC & _BV(dht_PIN)));
    }//end of "for.."
  return result;
}//end of "read_dht_dat()"