Arduino Beginners Course – Lesson 5

Arduino Beginners Course – Lesson 5

In this lesson we will explain how to use 2 new modules with Arduino! DHT11 to measure Temperature and Humidity and the Ultrasonic Sensor Module to measure distances!

Let’s Start!

What are we going to explain?

  • DHT11
  • Ultrasonic Sensor Module

DHT11

First of all we will learn what is and how to use DHT11 module.

DHT11 can measure Temperature and Humidity, later we will make some tests, but we can use this module to make also cool projects! You can find some projects about it searching in the search bar!

We explained this module in another article -> read here.

Read the article, then we will make here a sketch!

Before we can go on, you have to download the library for DHT11!

In the article you will find how to do, anyway if you know what to do, you can find it here.

So then include the library.

Now the you have read the article we can go on.

That’s the Schematic that you have to make.

From the left to the right

  • Arduino’s 5V -> “+” pin
  • digital pin5 -> out pin
  • Arduino’s GND -> “-” pin

DHT11 tutorial

//Libraries
#include <DHT.h>

//Constants
#define DHTpin 5     // which pin we're connected to
#define DHTtype DHT11   // DHT 11
DHT dht(DHTpin, DHTtype); // Initialize DHT sensor


//Variables
float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup()
{
  Serial.begin(9600);
  dht.begin();
  delay(1000); //the module needs a while before working properly
}

void loop()
{
    //Read data and store it to variables hum and temp
    hum = dht.readHumidity();
    temp = dht.readTemperature();

    //Print temp and humidity values to serial monitor
    Serial.print("Humidity: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.println(" Celsius");
    delay(2000); //Delay 2 sec.
}

Ultrasonic Module Sensor

Now let’s have a look at the Ultrasonic module sensor!

With this module we can measure distances, but we explained it in another article, so please -> read here Ultrasonic Module Sensor.

There we explain how it works and we say more details!

Now that you have read the article, we can write a simple code here.

Make this simple circuit!

Ultrasonic module schematic

const int pinTrig = 11;
const int pinEcho = 10;

const float speedSound = 0.034;               /*we use a const float because 
                                              0.034 is a decimal numbers*/
long lenght;
int distance;

void setup()
{
  pinMode(pinTrig, OUTPUT);
  pinMode(pinEcho, INPUT);
  Serial.begin(9600);
}

void loop()
{
  digitalWrite(pinTrig, LOW);
  delayMicroseconds(5);                       /*sets the pinTrig LOW for 5 Microseconds (then you will 
                                              understand why we use DelayMicroseconds)*/
  digitalWrite(pinTrig, HIGH);
  delayMicroseconds(10);                      //sets the pinTrig HIGH for 10 Microseconds
  digitalWrite(pinTrig, LOW);                 /*sets the pinTrig LOW because now we set 
                                              HIGH the pinEcho to obtain the distance*/
  lenght = pulseIn(pinEcho, HIGH);            /*the function pusleIn reads a pulse on a pin and it returns 
                                              the length of the pulse in microseconds. That's why we 
                                              used delayMicroseconds*/
  distance= lenght*speedSound/2;              /*The distance between the sensor and the object is only 
                                              the of the distance that the waves traveled.
                                              So we need to divide the result by 2.*/
  Serial.print("Distance: ");                 /*Prints the distance on the 
                                              Serial Monitor*/
  Serial.println(distance);
}

In the article it is explained step by step and you will understand more abou it!

That’s all! when you will read all the lessons you will know how to use a lot of modules with Arduino!

Davide Busco

Davide Busco is an engineering student at Federico II University of Naples. He uses his passion and studies to create his projects. He loves be a maker and spread his knowledge. Moreover he has gained the second place in a national makers-tournament between Italian's Universities.

Leave a Reply

Your email address will not be published. Required fields are marked *

en_GBEnglish (UK)
it_ITItaliano en_GBEnglish (UK)
%d bloggers like this: