Hello World for Sensors With Arduino

You have obviously heard of the Arduino boards
and how they have revolutionized hobby electronics.Here is a little primer to get you started.

you have heard a lot about 'Internet of Things' and probably bought yourself an arduino kit along with a few more sensors but you can't figure out how exactly use them! If that sounds like you,then you are at the right place.
Here's what you will need:
  • An Arduino uno(Obviously!)
  • Jumper wires(Make it yourself!)
  • Breadboard(Optional,but helpful)
  • Arduino IDE
There are undoubtedly a lot of sensors available and covering all of them wont't be feasible and that's why I'll cover the most frequently used sensors.so let's get started!

Ultrasonic Distance Sensor
You would have probably read about the SONAR that's used in ships.By taking the same concept into a small form factor we get a distance sensor.This sensor is available in multiple variants and I'll be using the 'HC-SRO4' variant.



 

It can easily detect range between 2 to 450 cm and has 4 different pins.To brief you a little, the 'trigger' pin will send the pulse and the 'ECHO' pin would wait for it to return.The other two pins,GND & VCC, are common and found on almost every component,'GND' pin stands for 'ground' and the 'vcc' pin means '5v input'.
        The connection is fairly simple;you need to connect the sensor to the Arduino directly (you may also use a breadboard).Using the jumper wires,connect the 'GND' pin to the 'GND' port of the Arduino and connect the 'VCC' pin to the "5v' port on your Arduino.The 'TRIG' and and'ECHO' pin can be connected to any of the Arduino's Digital pins.For now,I'm connecting 'TRIG' to Digital pin no. 8 and the 'ECHO' to pin no. 7.And dont forget to connect the Arduino to your computer using the USB cable.



   The beauty of these sensors is that most of them have a library function available, which basically ease our work since we dont need to re-invent the wheel.The library for this sensors is named as 'New Ping' and you can get it from http://bit.ly/1pys3Ov and then unzip it into the documents-->Arduino-->libraries folder.Open up the Arduino IDE (or restart in case it was already open) and on the menu bar,navigate to File-->Sketchbook-->libraries-->New Ping-->New Ping Example.Clicking on it would open up another Arduino window that has got all the code necessary to use the Distance Sensors.Click on the upload button and a few LEDs would blink on the Arduino. After 2-3 seconds open up the "Serial Monitor" under "Tools" menu option and there you go!you'd be seeing the distance between the sensors and the object in front of it.Using a flat surface object will get you better results.
      You don't happen to write any code this time since most of it is handled y the library.However,if you've notice then the distance is actually returned by the function named 'sonar.pingcm()'.So yeha,go on,assign that to a variable and use he variable for your programs.You can make a car that turns automatically if something comes within 'x' units(see my another tutorial http://vicktechx.blogspot.in/2016/04/how-to-make-simple-obstacle-avoiding.html) or maybe,automate the lights in your home,so that if something comes within 'x' units then turn on off the lights.


PIR Motion Sensor


 Talk about detecting motion and here we have our RS.100 sensor that will detect motion for you.As the name suggests,PIR stands for Passive Infrared and it works by detecting differences infrared lights emitted by objects.As soon as it notices any difference in infrared levels,it registers it as motion.It comes with three pins VCC(5 volts),GND and OUT.
    The connection is again pretty simple.connect the VCC pin to the '5 volts' port on the Arduino and the 'GND' pin goes into the GND port.IN fact,these are some conventions that you must get used to by now because you will encounter these two pins in almost every sensors.And the  other pins are generally connected to the Digital pins and in the PIR sensor,connect the 'OUT' pin to the Digital pin no. 2 (you can choose other ports too.It's totally your choice,just make sure to make relevant changes in the code).
    Open up the Arduino IDE and enter the codes as given on http://pastebin.com/AE35ac9N. This code will turn ON or OFF the onboard LED light if any motion is detected.So,edit that as per your program's requirement and you'll start using the sensor.


Temperature and Humidity Sensor

Almost every variant must have at least three pins,each for 5 volts,ground and signal respectively.

  They work by calculating the electrical resistance between the two electrodes that is present beneath it but you need not worry about that,rather,let's get familiar with its connections.Chances are that instead of 'VCC' or 'GND',you may notice that it has plus and minus signs marked respectively.'SIGNALS' pin would be marked with an 'S'.This time,try to connect them with Arduino on your own(after all,you won't blow it up).For now,we will connect the SIGNAL pin to the DIGITAL pin no.7.    
   Fortunately,we've got a library function for this sensor which you can download from http://bit.ly/1PPAiZZ and extract it innto the Documents-->Arduino--> libraries folder.Get back to Arduino IDE and get the sample code from http://pastebin.com/tRaThA9h. Hit the upload button and open up the serial monitor and you will see the temperature and the humidity coming up.

Relay

   This exactly isn't a sensor but it useful as it acts as an electric switch and you would be using it often.It generally comes in multiple channels which translate into"how many individual switches you want?"Buying a '5 volt 4 channel relay' would be ideal for most of you.It will have 4 pins,1 for each switch and 2 more pins for GND and 5 volts.On the other side,the relay's switch will contain 3 ports. COM stands for common. NO stand for Normally Open and NC means Normally closed.One end of the wire will go in the 'common' port and the other in either of the remaining two ports.NC or NO,depending on your usage.
    Library files for this one would make no sense since all it needs to do is to accept either HIGH(1)  or LOW (0) commands from Arduino. You can copy the code  from http://pastebin.com/cW15YBU5.
According to the code,if the temperature is above 20 it will turn ON the appliance(if doesn't then you'll need to put the 'second' wire into NO port).
   And guess what,you just used two different sensors to build a project that is intuitive enough to take surrounding's temperature and 'react' accordingly.Likewise,there are other tons of availabe sensors which you can combine together and create something more awesome.Happy DIY!

Comments

Popular posts from this blog

How to make a simple line following robot using Arduino uno

How to make a simple obstacle avoiding robot using Arduino uno

Why iPhone 7 Don't Have A Earphone Jack