Showing posts with label Sensors. Show all posts
Showing posts with label Sensors. Show all posts

How to Use a Vibration Sensor or Shake Switch with Arduino Uno

In this post we will look at how to use a Vibration Sensor (also known as a Shake Switch) with the Arduino Uno. A common Vibration Sensor or Shake Switch used with the Arduino Uno has a technical model number known as SW-18015P or SW-18020P. We can also refer to them as SW-180 series Vibrator Sensors or Shake Switches.

Vibration Sensor or Shake Switch SW-18015P / SW-18020P
Figure 1 - Vibration Sensor or Shake Switch SW-18015P / SW-18020P













As shown above, a Vibration Sensor or Shake Switch looks like a cylindrical capacitor in shape.

The Vibration Sensor or Shake Switch
Inside the Vibration Sensor or Shake Switch there is a complex mechnical spring system wound around a central shaft. The following diagram based on the SW-180 vibration sensor series datasheet depicts the working principle of a vibration sensor.

Working principle of a Vibration sensor or Shake switch
Figure 2 - Working principle of a typical Vibration sensor or Shake switch















The internal spring mounted system moves when the sensor is acted uopn by external stimuli. The resultant movement within the Vibration Sensor or Shake switch results in a change in electrical voltage. This change in voltage can be measured by any micro-controller unit such as an Arduino Uno. Let us see how in the next section.

Parts Needed for Vibration Senbsor or Shake Switch with Arduino
You will need the following items for using a Vibration sensor or Shake switch with the Arduino Uno:
  1. Arduino Uno R3 board
  2. USB-A to USB-B cable
  3. A SW-180 series vibration sensor or shake switch
  4. A breadboard
  5. Some jumper wires

The Breadboard Circuit for Vibration Senbsor or Shake Switch
The breadboard setup for connecting the vibration sensor with the Arduino Uno is shown below for reference.

Using Vibration Sensor (Shake Switch) with Arduino Uno
Figure 3 - Using Vibration Sensor (Shake Switch) with Arduino Uno


















The connection are explained below for easy understanding.

  1. Connect one leg of the vibration sensor or Shake switch with Arduino Uno's analog pin A5
  2. Connect the other leg of the vibration sensor or Shake switch with Arduino Uno's 5V power supply pin

The Arduino C Sketch for Vibration Senbsor or Shake Switch
After the circuit has been built, use the following C sketch to interface the Vibration sensor or Shake switch with the Arduino Uno. The main point to note here is that when the sensor does not vibrate then the analog values will be greater than 1022. So in order to react to vibration pay attention to the logic writen in the if statement below.

// variable to store analog value read from the vibration sensor
int sensorReading;
int sensorPin = A5;

void setup()

{
   Serial.begin(9600);
}

void loop()

{
   sensorReading = analogRead(sensorPin);

   // normally when nothing is vibrating or shaking

   // the sensor values on the analog pin A5
   // will be between 1023 to 1024
   if (sensorReading < 1022)
   {
      Serial.print("Things started moving: ");
      Serial.println(sensorReading);
   }

   // check every 5 seconds
   delay(5000);
}

Interesting Applications of the Vibration Senbsor or Shake Switch
The SW-180 series vibrations switch can be utilized for building some interesting real worl applictaions such as:
  1. Anti-theft alarms
  2. Monitoring moving machinery/equipment
  3. Instruments / Toys
  4. Sport Equipments
Conclusion
This is how a SW-180 series vibration switch or shake switch is interfaced with an Arduino Uno.
This example has multiple fundamentals such as: reading analog I/O, fundamentals of interfacing with sensors, using sensor datasheets, using the Serial Monitor for sketch debugging and so on. These and many other things have been covered in detail in my book "Learn Arduino Prototyping in 10 Days".


If you liked this post, then your encouragement by performing a G+ and a share is most welcome and highly appreciated!

Temperature Sensor with Arduino Uno


In the previous post we saw how to measure temperature using the DHT11 temperature sensor with the Arduino UNO. In this post we will discuss and see how to use a basic temperature sensor for detecting higher than normal or "fire-like" heat/temperatures (a potential emergency). As usual we will use our favorite prototyping equipment, the Atmel chip based Arduino Uno (you may use an 8-bit PIC family processor also as an alternative, but then you will have to use MPLAB for burning the C programs and some USB to TTL programmer hardware) for writing the embedded C programs to interface with the sensors.

Which Temperature Sensor should I use?

Well, it is tricky. There are a myriad of temperature sensors available in the industry that may be used with an Arduino UNO. Each of them differ mainly on two grounds: accuracy and operating conditions. If you are planning the devise a particular IoT solution at a particular geographical location or weather condition, then carefully evaluate the datasheets of these sensors. A snapshot image of six commonly used temperature sensors is provided below.

DHT11 Temperature Sensor with Arduino UNO
Figure 1 - Commonly used Temperature sensors.
For the purpose of this demonstration, I will use the DHT11 temperature sensor with Arduino UNO, partly because I had it lying around from an old kit. However, this sensor is not very accurate as it can give readings that are off by 2 degrees celcius (+/-). Also DHT11 can measure a maximum of 50 degrees celcius, so it is not a very good detector of "fire-like" heat. Instead use DHT22 (based on DS18B20) has better range for sensing "fire-like" temperatures. You may refer to the DHT11 datasheet here. By the way, the DHT11 can also be used to sense relative humidity, however, we will not use this feature for our Smart City Emergency Response System project. For a more accurate temperature sensing, you may evaluate the datasheets of other sensors before employing them. Just search for the term - "DHT22 datasheet", for example, in google/bing.

How to connect/wire a DHT11 Temperature sensor with an Arduino?

We will be using the DHT11 temprature sensor with Arduino UNO for our experiment. Given below is my account of using this sensor. The DHT11 sensor has 4 pins (remember Pin 3 in not used), the wiring for the sensor and the Arduino board is shown below. One important point to note is the mention of connecting a pull-up resistor, as shown in the snapshot taken from the DHT11 datasheet in the Figure  below. This point is a source or some confusion, as I found in a lot of forums over the internet. However, it is as simple as just joining Pin 1 to Pin 2 using a 5K resistor (as shown in Figure 3 below), if the length of the cables is below 20 mtrs (which was the case in my experimental setup).

DHT11 Temperature Sensor with Arduino UNO
Figure 2 - Suggested Wiring from DHT11 Datasheet
Given below is how I wired the DHT11 sensor to my micro-controller board the Arduino Uno.

DHT11 Temperature Sensor with Arduino UNO
Figure 3 - Actual wiring of a DHT11 temperature sensor with an Arduino board

You should connect them as follows:

DHT11 Sensor          Arduino
----------------          ----------
Pin 1                 --->   5V
Pin 2                 --->   Pin 2
Pin 3                 --->   (not used)
Pin 4                 --->   GND

NOTE - Remember to connect the resistor between Pin 1 and Pin 2 of the DHT11 sensor.


Embedded C Program for reading the DHT11 Temperature sensor

There is a pre-requisite of installing the DHT11 library, for using the DHT11 temperature sensor with Arduino UNO. Follow these steps to first install the DHT library. You will need this library in order to include the DHT.h file in the sketch below.

  • Launch the Arduino IDE.
  • Navigate to Sketch > Include Library > Manage Libraries...
  • The Library Manager window will pop up.
  • In the Library Manager window, search for DHT and install the required library. The library that I choose to install is shown below.
DHT11 Temperature Sensor with Arduino UNO
Figure 4 - Screenshot of Installed DHT Sensor Library in Arduino IDE Library Manager


Next you may use the following sketch for reading values from the DHT11 temperature sensor with Arduini Uno. The below code reads the DHT11 temperature sensor values at 5 second intervals and checks whether the temperature exceeds 65 degrees celcius (or 149 degrees farenheit). However, proper calibration and consultation with fire experts is necessary for a working threshold for fire temperatures.



Conclusion

To conclude this post, we have seen how yu may use a DHT11 temperature sensor with the Arduino UNO platform. Once the alert has been detected, you will have to take some suitable action such as (but not limited to):
  • Sounding an alarm
  • Posting the alert to an IoT cloud platform
  • Activating some defence mechanism (such as sprinklers in case of fire)
  • Sending SMS and calling phone numbers

Smoke Detector with Arduino Uno

In this post we will discuss and see how to use a Smoke Detector / Sensor with Arduino Uno for detecting smoke. As usual we will use our favorite prototyping equipment, the Atmel chip based Arduino Uno (you may use an 8-bit PIC family processor also as an alternative, but then you will have to use MPLAB for burning the C programs and some USB to TTL programmer hardware) for writing the embedded C programs to interface with the sensors. Before jumping into the wiring and code for using a Smoke Detector / Sensor with Arduino Uno, a short video of my experiement with smoke detector / sensor using an incense stick is provided below. In the video you will notice that once smoke is detected, an alert saying "Smoke detected" is displayed in the serial window of the Arduino IDE.


Connect a Smoke Detector / Sensor with an Arduino Uno?

We will be using the MQ-2 series Gas/Smoke detector / sensor with Arduino Uno, for our experiment. The MQ-2 sensor has 4 pins, the wiring for the sensor and the Arduino board is shown below. In case you want to use the Digital output pin (D0) then remember to use the gas sensitivity adjustment potentiometer (can be found on the reverse side of the chip) as per your requirement. Otherwise, if you are using the Analog output pin to read the sensor then no need for using the in-built potentiometer. Use the gas sensitivity graph on Page 2 of the datasheet here to play around with the threshold values in the program below. Not only gas/smoke, but you can also detect other types of gases. Given below is my account of using this sensor. For more details on gas sensor you may refer this site.

MQ2 Smoke Detector / Senor with Arduino Uno
Figure 1 - Wiring an MQ-2 Smoke detector / sensor with an Arduino Uno


You should connect them as follows:

MQ-2 Sensor          Arduino
----------------          ----------
Vcc                 --->   5V
GND               --->   GND
D0                   --->   (not used)
A0                   --->   A0

Embedded C Program for reading the Smoke detector / sensor with Arduini Uno

You can use the following sketch for reading the Smoke detector / sensor with Arduini Uno. However, do some self testing and calibration with the threshold values.


Conclusion

Using the above example you should be able to use the MQ2 Smoke detector / sensor with and Arduino Uno. Once the alert has been detected, you will have to take some suitable action such as (but not limited to):
  • Sounding an alarm
  • Posting the alert to an IoT cloud platform
  • Activating some defence mechanism (such as sprinklers in case of fire)
  • Sending SMS and calling phone numbers

Arduino - Reverse Car Parking Alarm

As promised in the previous post, we will be building a Reverse Parking alarm for cars. For doing so we will utilize the same wiring scheme as in my previous post. However, we will change the C sketch slightly. The change in the C code will be to start beeping the Piezo Buzzer at longer intervals when the Car is 30 centimeters (1 feet) away from an object, followed by intensifying the beeping at shorter intervals as the Car comes closer to the obstacle. Watch the Youtube video of the sample project below.



Setup the wiring for the reverse parking alarm as described in my previous post. Followed by using the C program written below.


Hope you enjoyed this article! Please share and comment on this post if you found it useful. I would love to hear from you.

Cheers,
KBRC

Arduino - How to detect obstacles/objects and react?

How to detect an obstacle with an Arduino and HC-SR04 Ultrasonic Module?

In this post we will detect an obstacle using the HC-SR04 Ultrasonic Ranging module with an Arduino. Basically, we will write an embedded C program for Arduino to read the Ultrasonic sensor data every second, followed by converting the sensor data into distance and in the end run a piece of logic that if the detected object (i.e. motion due to moving object) is within 10 centimeters then a Piezo Buzzer will be beeped. Check out the Youtube video below demonstrating my experiment with detecting motion.





The breadboard setup for detecting an obstacle has been provided below as a reference. You may use this wiring scheme for hooking up your HC-SR04 along with the Piezo Buzzer to your Arduino. Be careful not to connect the wrong wires, as it might result in a short circuit.


Figure 1 - Breadboard Wiring for obstacle detection 

The figure below shows the schematic wiring diagram for detecting an obstacle.


Figure 2 - Schematic wiring for obstacle detection

Join the wiring as follows:
  • HC-SR04: Connect Vcc (on HC-SR04) to 5V (on Arduino)
  • HC-SR04: Connect Gnd (on HC-SR04) to Gnd (on Arduino)
  • HC-SR04: Connect Trig (on HC-SR04) to Pin 4 (on Arduino)
  • HC-SR04: Connect Echo (on HC-SR04) to Pin 12 (on Arduino)
  • Piezo Buzzer: Connect the + Terminal (Longer leg) to Pin 8 (on Arduino)
  • Piezo Buzzer: Connect the - Terminal (Shorter leg) to Gnd (on Arduino)

That C sketch for detecting an obstacle with the Arduino is very simple and is provided for reference below.



Hope you enjoyed this post. Please share this post if you found it useful and write your comments below this post. I would love to read your your thoughts and experiences with the HC-SR04 Ultrasonic Ranging module.

In my next post, I will show you how you can use your Arduino and HC-SR04 to create a reverse parking alarm for a car!

Cheers!
KBRC

Arduino - How to measure distance?

How to measure distance using Arduino?

In this post we will look at measuring distances using an Ultrasonic sensor with the Arduino. For this purpose, I have chosen a commonly available sensor module called the HC-SR04 Ultrasonic Ranging Module. There are other Ultrasonic sensor as well, that give better field performance. However, we will start off with the HC-SR04. Given below is a picture of the HC-SR04 module. The HC-SR04 is an ultrasonic ranging module capable of measuring distances using Ultrasonic sound waves and it can be interfaced with not only the Arduino, but also various micro-controllers.


In one of my future posts, I will explain how we can hook  up the HC0SR04 with an ESP8266 Wifi Module and make it behave like a local web server, thereby reducing the costs of having a costly edge device for data acquisition.

Figure 1 - The HC-SR04 Ultasonic Module

There are various applications that may be developed using this Ultrasonic module with the Arduino. In these series of posts, we will analyze this module with the Arduino from various angles: Basic programming, Specifications Testing and then look at some Automation Solutions. For this post we will only look at basic distance measurement.

Basic Programming and Interfacing

The basic programming is straight forward and has two steps: setting up the circuit and writing the sketch/program. Build the circuit as shown below. This is a schematic provided to help you in wiring up.


Figure 1 - Wiring Schematic Diagram

Connect the PINS as described below:
  • Connect Vcc (on HC-SR04) to 5V (on Arduino)
  • Connect Gnd (on HC-SR04) to Gnd (on Arduino)
  • Connect Trig (on HC-SR04) to Pin 8 (on Arduino)
  • Connect Echo (on HC-SR04) to Pin 12 (on Arduino)


Next use the following sketch. Compile and load the following into your Arduino.



In order to test the system, keep your Arduino plugged in with your computer, and open your Arduino IDE's Serial Monitor window (Tools > Serial Monitor) - the serial monitor will show you the output from the print() and println() function calls in the running sketch. The running sketch will keep reading the sensor data, do some calculations and print the distance of the object in front of the sensor at regular intervals. Place/move some objects in front of the sensor and keep watching the serial monitor window.

See for yourself what gets printed on the Serial Monitor window, as you move the object(s) away from the sensor and bring them near the sensor. I will be eager to read and learn from your experiences, so please put your comments below.

In the next post we will see how to detect an obstacle and react using this Ultrasonic module.


Cheers,
KBRC