Lab 3.1: Arduino Sensors

Overview

The most fun you can have (after blinking LEDs) is using sensors to detect what’s going on in the world and act on that information. However, all sensors have their own methods of interfacing. That can make them a real pain to work with. Luckily, Arduino makes it a lot easier to work with sensors. In this lab, we’ll work with a few sensors in order to get better familiarization with the Arduino hardware and software environment. Please check which two sensors have been provided to you and jump to lab sections for those sensors. Once you’re finished working with your sensors, you can temporarily switch it with another student for a different kind of sensor. Try to work with at least 2-3 sensors.

 

Temperature Sensor

This is a multifunctional sensor that gives you temperature and relative humidity information at the same time. It provides reliable readings when environment humidity condition are between 20% RH and 90% RH, and temperature condition between 0°C and 50°C, covering needs in most home and daily applications. Typical applications of this sensor includes Personal weather station and Humidity regulator etc. To read temperature and humidity information from Temperature and Humidity Sensor, do the following:

  • Mount the Shield on the Arduino and connect the Temperature and Humidity sensor to a digital port (e.g. D2) of the Shield as shown in the figure. This will connect pin 1 (on the left) of the sensor to +5V, pin 2 of the sensor to whatever your DHTPIN is defined to be, and pin 4 (on the right) of the sensor to GROUND.

  • Download Humidity_Temperature Sensor Library. In Arduino IDE, click on the menu bar item (Stetch => Import Library => Add Library), navigate to directory where the downloaded DHT.zip is located. Select DHT.zip and click OK. In Arduino IDE, open a new sketch (go to File => New ) , and do the following:

First we have to specify what pin we’re connected to. We also need to specify the sensor type are we using (DHT 22 in our case). We do this through #define, which is a useful C component that allows the programmer to give a name to a constant value before the program is compiled. Defined constants in arduino don’t take up any program memory space on the chip. The compiler will replace references to these constants with the defined value at compile time. The third statement binds the the pin we specified to the type of sensor we have.

Every Arduino program comprises of two functions, setup() and loop(). setup() runs once when you turn your Arduino on to perfrom initialization related tasks, while loop() runs indefinitely until the Arduino is powered off. In setup(), we first initialize the serial connection with the computer so that we can fetch the temperature/humidity data and display it via Arudino serial terminal. Remember that we need to specify the rate at which data, received through serial port, is sampled by host computer, which must be the same as the rate at which data is sent by the Arduino. We do this by calling Serial.begin() with the parameter 9600 which the speed (in bits per second) at which Arduino sends data. The final initialization step is to call dht.begin() so that we can start sampling sensors.

The loop() function will do the desired task forever until you turn off the Arduino board. The loop is quite straight forward. All it does it read the temperature and humidity values from the sensors and send it to host computer through serial port where it’s printed via serial monitor.

After successfully uploading the sketch, open the serial monitor (go to Tools => Serial Monitor), where you can the see temperature/humidity data.

 

Infrared Sensor

PIR sensors allow you to sense motion, almost always used to detect whether a human has moved in or out of the sensors range. They are small, inexpensive, low-power, easy to use and don’t wear out. The PIR sensor we have is a simple to use PIR motion sensor with Grove compatible interface. Simply connect it to the shield and program it. Typical applications of this sensor are motion sensing, thief-guarding System, and industrial automation etc.

The following sketch demonstrates a simple application of sensing motion. When someone moves in its detecting range, it outputs High through its SIG pin and the LED lights, else it outputs LOW. Then you can use it to detect the motion of people.

Mount the Mega Shield on Arduino ADK, connect the PIR Motion Sensor to a digital port, and connect the Arduino board to PC via a USB cable as shown below:

Copy and paste code below to a new Arduino sketch. To open a new sketch (go to File => New ).

Try to understand the simple code, then upload the code and test if it can sense your motion.

Note: The detecting distance can be adjusted by rotating the distance potentiometer. When you rotate the distance potentiometer clockwise, the detecting distance decreases from 6 meters to only several centimetres. And when you rotate it counter-clockwise to the end, the module will be too sensitive to be triggered by the atmosphere even there is no people moving before it. The holding time can also be adjusted by the Delay_time potentiometer, the value is about from 25s to 1s when you rotate it clockwise.

 

Ultrasonic Sensor

This Grove-ultrasonic sensor is a non-contact distance measurement module which is designed for easy modular project usage with industrial performance. It has a detection range of 3cm-4m with 1 cm accuracy and works best in 30 degree angle. The Ultrasonic Ranger will find a place in numerous projects :presence detector, robotics, car parking, distance measurement.

Firstly, download the library Ultrasonic.zip . In Arduino IDE, click on the menu bar item (Stetch => Import Library => Add Library), navigate to directory where the downloaded .zip is located. Select .zip file and click OK.

The following code demonstrates a simple application of using the sensor to measure distance. Copy and paste code below to a new Arduino sketch. To open a new sketch (go to File => New ). This sketch reads a ultrasonic range finder and returns the distance to the closest object in range. To do this, it sends a pulse to the sensor to initiate a reading, then listens for a pulse to return. The length of the returning pulse is proportional to the distance of the object from the sensor.

Take some time to understand the code, then upload it to the Arduino board, open serial monitor (SHIFT+CTRL+M) and test if it’s working.

 

Sound Sensor

The Sound sensor module is a simple microphone. The following sketch demonstrates a simple application of the sound sensor to control the led. Connect the sensor  to analog port A0 of the ADK shield and LED to digital port 13. The potentiometer is used to regulate the gain of the output signal. The larger the potentiometer, the larger the output signal. If the sound of the environment is bigger than the threshold, then the Led will be turned on.

 

Extend the above code so that if the value sensed by the sound sensor is higher than the threshold, display the value on the serial monitor.

 

Thumb Joystick

Thumb Joystick is a Grove compatible module which is very similar to the ‘analog’ joystick on PS2 (PlayStation 2) controllers. The X and Y axes are two ~10k potentiometers which control 2D movement by generating analog signals. The joystick also has a push button that could be used for special applications. When the module is in working mode, it will output two analog values, representing two directions.Compared to a normal joystick, its output values are restricted to a smaller range (i.e. 200~800), only when being pressed that the X value will be set to 1023 and the MCU can detect the action of pressing. It requires us to use the analog port of Arduino to take the readings.

  1. Connect the module to the A0/A1 of ADK Shield using the 4-pin grove cable.
  2. Plug the Shield into Arduino.
  3. Connect Arduino to PC by using a USB cable.

 

Chainable RGB LED

You can use the Chainable RGB LED as follows.

  1. Connect the Grove socket marked “IN” on the Chainable RGB LED and D7/D8 on the ADK shield. You can change to the digital port as you like, but don’t forget to change the port number in the definition of the demo code at the same time.
  2. The 2 Grove interfaces in Grove – Chainable RGB LED are respectively screen-printed “IN” (for controlling data input) and “OUT” (for controlling data shared with the next Chainable RGB LED).
  3. Plug ADK Shield onto Arduino/Seeeduino.
  4. Download the Chainable RGB LED Library. In Arduino IDE, click on the menu bar item (Stetch => Import Library => Add Library), navigate to directory where the downloaded .zip is located. Select .zip file and click OK.
  5. Run the following demo code:

 

Digital Light Sensor

This module transforms light intensity to a digital signal. You can switch between three detection modes to take your readings. They are infrared mode, full spectrum and human visible mode. When running under the human visible mode, this sensor will give you readings just close to your eye feelings. Follow these steps to use this sensor:

  1. Plug the Digital Light Sensor onto the I2C ports (pins 20 and 21 on ADK shield), and then plug the shield onto Arduino ADK.
  2. Download the library Digital Light Sensor Library.
  3. In Arduino IDE, click on the menu bar item (Stetch => Import Library => Add Library), navigate to directory where the downloaded .zip is located. Select .zip file and click OK.

Create an Arduino sketch and paste the below code to it

 

Accelerometer

This 3-axis Accelerometer module can be used for sensing data changes, product orientation, and gesture detection. It is a very low power, low profile capacitive MEMS sensor. Typical applications include

  • Motion Detection of Robot
  • Mobile Phone/ PMP/PDA: Orientation Detection (Portrait/Landscape),Image Stability, Text Scroll, Motion Dialing, Tap to Mute

Follow these steps to get raw data from this sensor.

  1. Connect this module to the I2C port of Shield (e.g. pins 20,21 ).
  2. Download the library; And unzip it into the libraries file of Arduino IDE by the path: ..\arduino-1.0.x\libraries.
  3. Open a new Arduino sketch and paste the following code to it

Speaker

The Grove- Speaker is a module which consists of power amplification and voice outputs. The loudness can be adjusted by the on-board potentiometer. With different input frequencies, the speaker generates different tones. The speaker can emit a variety of sounds like a car horn, doorbell and ignition . The different sounds are based on the frequency of the input signal. You can supply different frequency signal to this module with Arduino. Arduino generates these signals via PWM or even digital write and delay. Here we are going to show you how to generate these signals using delay(). Play with the numbers in array BassTab[] to get different tones.

Lab Exercise 3.1

Test all your sensors before leaving.