How to use sensors with Arduino

Curio Res
16 Aug 202005:41

Summary

TLDRThis tutorial guides viewers on connecting and plotting data from four sensors using an Arduino. It covers the DHT-11 for temperature and humidity, HC-SR04 for range, HC-SR501 for motion detection, and the GY521 for orientation assessment. The script details the process of wiring, using third-party libraries for sensor data interpretation, and visualizing data with a serial plotter. It also touches on adding additional sensors and emphasizes the ease of setup for most Arduino sensor breakouts.

Takeaways

  • πŸ”Œ Connect and plot data from four sensors: DHT-11, HC-SR04, HC-SR501, and GY-521.
  • πŸ”§ Start by powering the breadboard with the Arduino's 5V supply before connecting the DHT-11 sensor.
  • πŸ“š Use a third-party DHT sensor library to simplify reading temperature and humidity data.
  • πŸ’» Open a serial port and set the baud rate to 9600 bits per second to send sensor data from the Arduino to your desktop.
  • πŸ“Š Use the Arduino Serial Plotter to graph the data from the sensors.
  • πŸ“ Connect the HC-SR04 ultrasonic range sensor using the same method as the DHT-11 and install its library for range measurements.
  • πŸ‘οΈβ€πŸ—¨οΈ Wire the HC-SR501 motion detector similarly and use digitalRead to detect motion without needing a library.
  • πŸ”„ Connect the GY-521 sensor using I2C communication, and install the MPU6050 library to read orientation data.
  • πŸ”„ Use the getAngle commands to compute the GY-521's orientation, and test by rotating the sensor.
  • πŸ“Ί For more Arduino tutorials, subscribe to the Curio Res channel.

Q & A

  • What are the four sensors mentioned in the tutorial?

    -The four sensors mentioned are DHT-11 for measuring temperature and humidity, HC-SR04 for measuring range, HC-SR501 for detecting motion, and GY-521 for assessing orientation.

  • How should the DHT-11 sensor be connected to the breadboard?

    -The DHT-11 sensor should be connected with the positive terminal to the 5-volt rail, the negative to ground, and the OUT or S pin to any of the Arduino digital pins.

  • Why is it not necessary to write code to interpret sensor readings from scratch?

    -It is not necessary because third-party libraries are available that can be used to interpret sensor readings, simplifying the coding process.

  • What is the purpose of a third-party library in this context?

    -A third-party library provides pre-written code that can be used to interact with specific sensors, making it easier to read and interpret data from those sensors.

  • How can one find and install a library for the DHT sensor?

    -One can search for a DHT sensor library in the Arduino Library Manager, click 'More Info' to visit the repository, and then install the library from there.

  • What is the baud rate set for serial communication in the tutorial?

    -The baud rate is set to 9600 bits per second for serial communication.

  • What command is used to start the DHT sensor in the code?

    -The command used to start the DHT sensor is 'dht.begin()'.

  • Why is it important to wait for a tenth of a second in the loop function?

    -Waiting for a tenth of a second ensures that sensor readings are taken and communicated at regular intervals.

  • How can one view the output of the HC-SR04 ultrasonic sensor?

    -One can view the output of the HC-SR04 sensor using the Serial Plotter in the Arduino IDE.

  • What is the default unit of measurement for the HC-SR04 sensor's range?

    -The default unit of measurement for the HC-SR04 sensor's range is in centimeters.

  • How does the HC-SR501 motion detector work?

    -The HC-SR501 motion detector sends a 1 to the pin when motion is detected and a 0 when no motion is detected. It has a built-in delay that can be adjusted with screws on the sensor.

  • What communication protocol does the GY-521 sensor use?

    -The GY-521 sensor uses I2C (Inter-Integrated Circuit) communication protocol.

  • What library is recommended for reading from the MPU-6050 sensor?

    -The Talkin' MPU 6050 library is recommended for reading from the MPU-6050 sensor.

  • How can the sensor's orientation be estimated using the MPU-6050?

    -The sensor's orientation can be estimated using the accelerometer and gyroscope readings from the MPU-6050, specifically by using the 'get angle' commands.

Outlines

00:00

πŸ”§ Setting Up Sensors for Data Collection with Arduino

This paragraph provides a step-by-step guide to connecting and programming four different sensors with an Arduino. It covers using a DHT-11 sensor for temperature and humidity, an HC-SR04 for range detection, an HC-SR501 for motion detection, and a GY-521 for orientation assessment. The instructions start with wiring the DHT-11 sensor, including powering the breadboard rails with the Arduino's 5-volt supply. Users are advised to use a third-party library for interpreting sensor readings, and the process of installing and using the DHT sensor library is described in detail. Instructions also include setting up a serial port to send sensor data to a PC, using the Serial Plotter for data visualization, and coding practices for taking and printing sensor readings. Finally, users are guided on how to install and use libraries for other sensors like the HC-SR04 and the GY-521, along with advice on testing and verifying sensor readings.

05:01

πŸ“ Fine-Tuning Sensor Outputs and Learning More

This paragraph focuses on refining sensor output and understanding the specifics of each sensor's library commands. It emphasizes the importance of checking examples in the library documentation to ensure proper usage. The GY-521 sensor, which includes both an accelerometer and a gyroscope, is highlighted for its capability to estimate orientation. Users are encouraged to test the GY-521 by rotating it in different directions to see how the orientation data changes. The paragraph concludes with a prompt to subscribe to the Curio Res channel for further learning about Arduino and related projects.

Mindmap

Keywords

πŸ’‘DHT-11

The DHT-11 is a digital temperature and humidity sensor used in this tutorial. It is a basic, low-cost sensor that provides readings of temperature and humidity in the surrounding environment. In the video, it is connected to the Arduino to measure these parameters, and its data is later plotted using the serial plotter.

πŸ’‘HC-SR04

The HC-SR04 is an ultrasonic sensor used to measure distance by emitting sound waves and calculating the time it takes for them to bounce back. In the video, the HC-SR04 is connected to the Arduino, and its measurements are displayed in centimeters. The video emphasizes the simplicity of connecting this sensor similarly to the DHT-11 and how it provides accurate range measurements.

πŸ’‘HC-SR501

The HC-SR501 is a motion detection sensor that triggers when movement is detected. It sends a signal to the Arduino, which is then used to indicate the presence of motion. The video explains that this sensor is straightforward to use, requiring no additional libraries, and highlights its ability to detect motion and adjust the delay between detections using built-in screws.

πŸ’‘GY-521

The GY-521 is a sensor module that includes a gyroscope and accelerometer, used to assess orientation. This sensor communicates with the Arduino via I2C protocol. In the video, it is explained how the GY-521's readings can be used to estimate the orientation of an object by using specific commands from a library, such as 'get angle'.

πŸ’‘Arduino

Arduino is an open-source electronics platform based on easy-to-use hardware and software. In the video, it serves as the central unit that connects all the sensors (DHT-11, HC-SR04, HC-SR501, and GY-521) to collect data and send it to a computer for visualization. The tutorial guides users through wiring the sensors to the Arduino and programming it to interpret sensor readings.

πŸ’‘Serial Plotter

The Serial Plotter is a feature in the Arduino IDE that allows users to visualize data from the Arduino in real time. In the video, the Serial Plotter is used to graphically display the sensor readings, such as temperature, humidity, and range, making it easier to interpret the data collected from the sensors.

πŸ’‘Library

In the context of Arduino, a library is a collection of code that makes it easier to interface with sensors, displays, and other devices. The video mentions installing libraries for the DHT-11 and HC-SR04 sensors, which provide pre-written functions that simplify reading and interpreting data from these sensors. The tutorial emphasizes checking examples from the libraries to ensure they meet the user's needs.

πŸ’‘Breadboard

A breadboard is a tool for making temporary circuits without soldering, commonly used in prototyping electronics. In the video, the breadboard is used to connect the various sensors (DHT-11, HC-SR04, etc.) to the Arduino. It allows users to easily rearrange and test different circuit configurations as they follow along with the tutorial.

πŸ’‘Baud Rate

Baud rate refers to the speed of data transmission between the Arduino and the computer, measured in bits per second (bps). The video specifies setting the baud rate to 9600 bps to ensure proper communication when sending sensor data from the Arduino to the Serial Monitor or Plotter. Matching the baud rate in both the code and the Serial Monitor is crucial for correctly displaying the data.

πŸ’‘I2C Communication

I2C (Inter-Integrated Circuit) is a communication protocol used to connect low-speed devices like sensors to a microcontroller. The video highlights the use of I2C for the GY-521 sensor, where the SDA (Serial Data) and SCL (Serial Clock) pins are connected to the corresponding pins on the Arduino, enabling data exchange between the devices. This protocol is key to reading complex data like orientation from the sensor.

Highlights

Learn to connect and plot data from four sensors using Arduino.

Use a DHT-11 to measure temperature and humidity.

Utilize an HC-SR04 sensor for measuring range.

Incorporate an HC-SR501 motion detector for detecting motion.

Assess orientation using a GY-521 sensor, which includes an accelerometer and gyroscope.

Power the breadboard rails with the Arduino 5V supply before connecting sensors.

Search and install third-party libraries, like the DHT sensor library, from the Arduino Library Manager.

Include library headers and define sensor pins in the code to use the DHT sensor.

Use Serial.print and Serial.printline to send sensor data to the Arduino's serial port.

Set the baud rate to 9600 bits per second to match the data rate for serial communication.

Graph sensor data using the Arduino Serial Plotter.

Add additional sensors, like the HC-SR04 ultrasonic range detector, using similar wiring setups.

The GY-521 sensor communicates using I2C protocol, requiring specific wiring and library commands.

Adjust the delay of the HC-SR501 motion detector using the screws on the side of the sensor.

Test the GY-521 sensor by rotating it in different directions to assess orientation readings.

Transcripts

play00:00

in this tutorial you'll learn to connect

play00:02

and plot data from four

play00:03

sensors you'll use a dht-11 to measure

play00:06

temperature and humidity

play00:08

and hc sr04 to measure range and hc

play00:11

sr-501 to detect motion and a gy521 to

play00:16

assess

play00:16

orientation get started by hooking a

play00:18

dht11

play00:19

into the breadboard before wiring the

play00:22

sensor power the breadboard rails with

play00:23

the arduino 5 volt supply

play00:26

when you connect the sensor be sure to

play00:28

check the labels

play00:29

connect the positive terminal to the 5

play00:31

volt rail the negative to ground

play00:33

and the out or s pin to any of the

play00:35

arduino digital pins

play00:37

writing code to interpret sensor

play00:39

readings from scratch isn't necessary

play00:41

you can use a third party library

play00:42

instead search the library manager for a

play00:45

dht

play00:46

sensor library before selecting a

play00:48

library click

play00:49

more info to visit the repository where

play00:51

the library is stored

play00:53

once there look for examples these will

play00:55

show you how to use the library examples

play00:58

can also help you to identify if a

play01:00

library is right for your current skill

play01:01

level

play01:02

this one looks good go ahead and install

play01:05

the dht sensor library

play01:08

first include the library headers so you

play01:10

can use the library's classes and

play01:11

functions

play01:13

use a macro to define the sensor pen and

play01:16

an instance of the dht class from the

play01:18

dht library

play01:21

in order to send sensor information from

play01:22

the arduino to your desktop

play01:24

you need to open a serial port and set

play01:26

the data rate

play01:28

set the baud rate to 9 600 bits per

play01:30

second

play01:31

start the sensor with a call to

play01:33

dht.begin

play01:36

you can graph data received from the

play01:38

arduino on your pc using the serial

play01:40

plotter

play01:41

the serial.print and serial.printline

play01:44

commands write text and numeric data to

play01:46

the serial port as ascii text

play01:48

the first line printed to the serial

play01:50

port determines the legend on the plot

play01:52

so let's add some descriptive names

play01:54

using serial.print line

play01:56

in the loop function start by waiting

play01:58

for a tenth of a second

play01:59

this ensures that sensor readings are

play02:01

taken and communicated at regular

play02:03

intervals

play02:04

then take temperature and humidity

play02:06

readings from the dht object

play02:08

now that the readings have been taken

play02:09

print them to the serial line

play02:11

you'll also need to print a white space

play02:13

character finish by printing a new line

play02:16

character so that each set of readings

play02:17

appears on a new line

play02:19

now upload the code to the arduino and

play02:21

open the serial monitor

play02:23

set the baud rate to 9600 to match the

play02:25

data rate set in the code

play02:28

check that your legend is printed on the

play02:29

first line and each sensor reading shows

play02:31

in a new line below

play02:33

now that your data is streaming close

play02:35

the serial monitor and open the serial

play02:37

plotter to graph the data

play02:39

verify your sensor readings with a test

play02:42

most arduino sensor breakouts have a

play02:44

similar setup so adding additional

play02:46

sensors is easy

play02:48

let's add an hc sr04 ultrasonic range

play02:52

detector

play02:53

orient the sensor so that it is free

play02:55

from obstructions

play02:58

connecting the hc sr04 is just like

play03:01

connecting the dht-11

play03:03

the hc sr04 trigger and echo pins are

play03:06

wired to digital pins on the arduino

play03:10

install the ultrasonic library for the

play03:12

hc-sr04

play03:14

like the dht library the ultrasonic

play03:16

library requires you to include a header

play03:18

and define an instance of the ultrasonic

play03:20

class

play03:24

take a reading by calling the read

play03:26

function from the ultrasonic class and

play03:27

write the result to the serial line

play03:32

open the serial plotter to view the

play03:34

output of the ultrasonic sensor

play03:37

the default measurements from this

play03:38

library are in centimeters

play03:42

how accurate are the hc sr04 range

play03:45

measurements

play03:52

wire the hc sr501 motion detector using

play03:55

the same pattern you used to wire the

play03:58

dht-11

play04:01

the motion detector is so simple it

play04:03

doesn't need a library

play04:05

simply define the pin set it to input

play04:07

mode and call the digital read function

play04:11

when motion is detected the sensor will

play04:13

send a 1 to the pin otherwise it will

play04:14

send 0.

play04:16

add an entry to the legend and print to

play04:18

the serial line to complete the sensor's

play04:20

code

play04:21

the sr501 has a built-in delay between

play04:23

motion detections

play04:25

you can adjust the delay using the

play04:27

screws on the side of the sensor

play04:35

the fourth sensor is the gy521 breakout

play04:39

this sensor uses i squared c

play04:41

communication

play04:42

so wire the serial data and serial clock

play04:44

pins to the same pins on the arduino

play04:47

these pins are labeled sda and scl on

play04:50

both devices

play04:51

you'll also need to wire the sensor to

play04:53

the power rails

play04:55

install the talkin mpu 6050 library and

play04:59

follow the standard steps to read from

play05:00

the sensor

play05:02

the exact commands required depend on

play05:04

the library so always check the examples

play05:07

to learn which commands are necessary

play05:10

the mpu-6050 has an accelerometer

play05:13

and a gyroscope together these readings

play05:16

can be used to estimate orientation

play05:18

use the get angle commands to compute

play05:21

the sensor's orientation

play05:26

test the sensor by rotating it in

play05:28

various directions

play05:34

if you're interested in learning more

play05:35

about arduino subscribe to the curio res

play05:39

channel

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
ArduinoSensorsDHT-11HC-SR04HC-SR501GY521Data PlottingI2C CommunicationMotion DetectionOrientation AssessmentDIY Electronics