Course Content
The Arduino UNO
In the following three lessons, you will get to know your microcontroller - the Arduino UNO. You will learn more about its history, its functions, and most importantly: how to power it.
0/3
The Arduino IDE
For beginners, the Arduino IDE (Integrated Development Environment) is usually the first choice – and for good reason. You can program all Arduino boards with it and manage libraries for sensors, displays, etc. It also features the "Serial Monitor," where you can output data and troubleshoot.
0/2
Your first sketch
In the following lessons, you'll get to know the basic structure of an Arduino sketch and write your own programs. Let's get started!
0/2
The Serial Monitor
Now let's turn our attention to the Serial Monitor – a feature of the Arduino IDE that you will use in virtually every one of your projects.
0/2
Variables
No programmer can avoid variables. In the following lessons, you'll learn what types there are and what you can do with them.
0/3
Controlling an LED
Now it's time for more hardware! In the next lessons, you'll connect an LED to your Arduino. You'll first turn it on and off with a button. After that, you'll build a dimmer to control the brightness of the LED.
0/6
Lie Detector
Discover the entertaining side of electronics by building your own simple lie detector with your Arduino. This fun project uses basic components to measure skin resistance changes when someone might be telling a fib, perfect for adding some playful suspense to your next gathering with friends.
0/1
There’s music inside!
Your Arduino can do much more than "just" make LEDs shine at different brightness levels. For example, it can make music. In the following lesson, you'll learn how to use a piezo buzzer and coax some charming tones out of it.
0/3
A Theremin with Ultrasound
Do you want to make a bit more music? In this lesson, you'll build a theremin that you operate with your HC-SR04 ultrasonic sensor. You move your hand toward and away from the sensor - your Arduino calculates the pitch of the tones from the distance, which are then played through your piezo buzzer.
0/5
The Sound Sensor
Ready to explore how your Arduino can respond to sounds? In this lesson, we'll connect a sound sensor to your Arduino and learn how to make it respond to both digital noise detection and analog volume levels.
0/1
Build an Alarm System
In this project, you will build your own alarm system. It consists of three components: the sound sensor, which you have just learned about, the active piezo buzzer, and the RGB LED.
0/2
The DHT11 Temperature Sensor
Let's move on to another component that you'll certainly use in many projects: the temperature sensor. In this case, the popular DHT11, which can measure not only temperature but also humidity.
0/3
Arduino Course for Beginners

Now it’s getting serious: Using your new libraries, you’ll measure temperature and humidity with the DHT11 and display these values in your Serial Monitor.

Let’s first take a look at the libraries.

Importing Libraries in the Sketch

To use a library (that you’ve previously installed), you need to import it at the beginning of your sketch. This allows your program to access the functions stored in the library.

You can easily import the library for your sensor with a single line of code:

#include "DHT.h"

___STEADY_PAYWALL___

As you can see, you use the #include command followed by the name of the library with the extension .h in quotation marks. Important: Lines with #include should not end with a semicolon ; – an exception that would otherwise lead to an error.

You don’t need to import the Adafruit Unified Sensor library, as it only works in the background.

Often the names of libraries in the library manager and after the #include command are identical. In this case, they are exceptionally not the same – please don’t let this confuse you.

By the way: Almost all libraries install examples after installation. You can find them in the menu File -> Examples. For our library, select the entry DHT sensor library. There you’ll find example sketches with more information about importing and functions.

Specifying Connection and Sensor

So that the library knows which pin the sensor is connected to and which sensor (DHT11 or DHT22, the “bigger brother” of the DHT11) it is, you must first define these two parameters:

#define DHTPIN 4   
#define DHTTYPE DHT11

 

You also do this at the beginning of the sketch, right after importing the library. For this, you use the function #define – which, like #include, does not end with a semicolon ;.

Variables for Temperature & Humidity

To temporarily store your measurements, you need two variables – one for temperature and one for humidity:

float temp;
float humidity;

 

Since your sensor also measures tenths of a degree or percentage, these variables must be of type float.

Starting the Sensor

Next, you create an object based on the library called dht. You provide this instance with the connection pin and sensor type defined above:

DHT dht(DHTPIN, DHTTYPE);

 

Now follows the setup function. Here you first start the Serial Monitor and then the sensor:

void setup() {
  Serial.begin(9600);
  dht.begin();
}

 

Measuring and Displaying Values

Time for the loop. Here you can use two functions provided by the sensor’s library. With these functions, you store the current measured values in the respective variables:

temp = dht.readTemperature();
humidity = dht.readHumidity();

 

As you’ve probably already recognized, the function dht.readTemperature() reads the temperature and dht.readHumidity() reads the humidity. You can assign these values to variables as usual, which you can then use later.

The way the functions are written is interesting. At the beginning, you see the object dht – this “knows” by now which sensor you’re using and which pin it’s connected to. After that – separated by a dot – you access a “sub-function” provided by your object: either readTemperature() or readHumidity().

Next, you display your measurements in the Serial Monitor. You already know how this works from previous lessons. As you can see, the two variables temp and humidity are now used:

Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" *C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");

 

To ensure your sensor doesn’t rush from one measurement to the next, end the loop with a delay – e.g., two seconds:

delay(2000);

 

And that’s it! Upload the sketch from the Exercise Files for this lesson to your Arduino and open your Serial Monitor. You should now see the current temperature and humidity.

 

Exercise Files
measuring_temperature_and_humidity.zip
Size: 1.46 KB
We don't track you. Enjoy your cookies while making awesome projects!