Author Archives: Bosnia Bot

Interfacing DS18B20 temperature sensor to Arduino

Introduction

In this hands-on article, we will discuss DS18B20 temperature sensor from Maxim Integrated company. We will hook up this sensor to Arduino through amazing OneWire bus. Then we will write code that will bring our digital thermometer to life, and print temperature back to our computer. We will write generic code that will work on every Arduino platform.

DS18B20 Programmable Resolution 1-Wire Digital Thermometer

I choose to present you this sensor for several reasons. First of all, availability. You can buy this sensor online for 2-3$ (One Wire Digital Temperature Sensor – DS18B20).
Another good thing about this sensor is programmable resolution. That means, we can choose precision over refresh time, and vice versa. If wired correctly, this sensor can be powered from data line. More informations about this sensor can be found in its datasheet:

http://datasheets.maximintegrated.com/en/ds/DS18B20.pdf

Wiring

If we take a glance at DS18B20 datasheet, we can see that this sensor has three pins:

1. GND – Supply ground

2. DQ – Data line

3. Vdd – Supply 3.0V to 5.5V

So we just connect those pins to Arduino, and we’re ready to go, right? Wrong.
Generally, before we work with any analog or digital electronics, we should read it’s datasheet.

Quote from DS18B20 datasheet:

“The DS18B20 uses Maxim’s exclusive 1-Wire bus protocol that implements bus communication using one control signal. The control line requires a weak pullup resistor since all devices are linked to the bus via a 3-state or open-drain port (the DQ pin in the case of the DS18B20).”

So, we will have to add weak pullup resistor on DQ bus line. For 1-5 sensors, 4.7k ohm resistor.

When we hook-up everything, it should look like this:

temperature_diagram

Now, we’re ready to do some code!

Coding

Before we start coding, we must add OneWire library to our Arduino IDE. You can download lastest library here:
http://www.pjrc.com/teensy/arduino_libraries/OneWire.zip
Extract OneWire folder to libraries folder found in Arduino IDE folder. Now, write this code to new Arduino sketch. Explanations are in comments:

#include

int SensorPin = 10; //Our sensor is connected to digital pin 10

//Initiate OneWire communication
OneWire ds(SensorPin);

void setup(void) {
//Initiate serial communication with our computer
Serial.begin(9600);
}

void loop(void) {

float T = readT(); // call function to read from sensor
Serial.print(“Temperature is: “); // print out results to us
Serial.print(T);
Serial.println(” deg.C”); // print out results to us
delay(750);

}

float readT(){
//returns the temperature from our sensor

byte data[12];
byte addr[8];

if ( !ds.search(addr)) {
ds.reset_search();
return -300; // if there is no sensor on OneWire Bus, return -300 value
}

ds.reset();
ds.select(addr);
ds.write(0×44,1); // tell sensor to start converting
ds.reset();
ds.select(addr);
ds.write(0xBE); // tell sensor to start sending data

for (int i = 0; i < 9; i++) { // we receive data in this loop
data[i] = ds.read();
}

ds.reset_search();

byte MSB = data[1];
byte LSB = data[0];

float raw = ((MSB << 8) | LSB); // move MSB left for 8 spaces, join that with LSB
float realTempC = raw / 16; // move decimal point left for 4 spaces, result our temperature
return realTempC;
}

Testing

Now, let’s test our device! Open Serial Monitor at 9600bps, and you should have readings like this:

Temperature is: 28.31 deg.C
Temperature is: 29.25 deg.C
Temperature is: 29.56 deg.C

That’s it! Have fun extending it.

You can find more information and examples for this topic at site:

http://playground.arduino.cc/Learning/OneWire

Interfacing SirfStarIII GPS module sensor to Arduino

Introduction

In this hands-on tutorial we will hook up a GPS module to Arduino prototyping platform, and write complete Arduino code, step by step, with brief explanations. GPS modules are unlikely to work inside buildings, so we will show you few possibilities with powering your Arduino with batteries.

For this tutorial, we will be using PMB-648 GPS (SirfStarIII chipset) module:

http://www.parallax.com/Store/Sensors/CompassGPS/tabid/173/CategoryID/48/List/0/Level/a/ProductID/644/Default.aspx?SortField=ProductName%2cProductName

Don’t worry if you have other GPS modules, almost all of them use the same communications standard,  TTL or RS-232 asynchronous serial at 4800 bps. Do not worry about this right now, we will explain it into details later. Beautiful thing about this GPS module is that it has built-in antenna, do you do not have to worry about connecting one.

First of all, let’s take look at our PMB-648 specifications sheet:

http://www.parallax.com/Portals/0/Downloads/docs/prod/sens/PMB-648_Specification_V0.1.pdf

On first page, bottom-right section of the page, we can see the pinout of this module. As we can see, this module supports two standards for communications, RS-232, and TTL. We will use TTL standard, because Arduino is compatible with it.

WARNING: you should not, at any circumstances, connect RS-232 pins to Arduino! RS-232 voltage range is much higher than Arduino voltages, you will destroy onboard microcontroller!

If you want to learn more about RS-232 or TTL, you can read about it here:

http://en.wikipedia.org/wiki/RS-232

http://en.wikipedia.org/wiki/Transistor%E2%80%93transistor_logic


Wiring

What we are trying to accomplish is to connect Arduino to GPS module, read from it, and then send read data from Arduino to our computer (GPS -> Arduino -> Computer).

In this purpose, Arduino will talk to GPS using SoftwareSerial communication, and Arduino will report to us using his Hardware Serial. Do not worry if you do not fully understand this concept at this time, we will be demonstrating it soon.

Now, let’s connect complete circuit!

First, connect 5v power supply from Arduino to GPS Vcc and GND pins.

Second, connect GPS TTL TX pin to Arduino digital pin 2.

And, that is all of wiring we need. Whole process is illustrated below:

gps_connection

PMB-648 GPS module has LED on his board. This LED will start blinking when you apply 5v to module. This LED can indicate if GPS is locked on satelites:

-   Blinking: searching for satelites;

-   Constantly on: locked on satelites.

There is no need to connect GPS TTL RX pin to Arduino, because we will only read from it. We do not have need to transmit data to GPS.

Notice: there are some configurations on GPS module that can be accessed through it’s TTL RX pin. For most of the time, default configurations are what we need.

Coding

Now, our task is to write some code to bring our masterpiece to life.

What we need to to is very, very simple. We want to set Software Serial communication on digital pins 2 and 3 (notice on schematics that we connected GPS TTL TX on pin 2), and we want arduino to print exactly the same data to us, on Arduino Serial monitor. Here is the complete code:

#include <SoftwareSerial.h>

SoftwareSerial GpsMod = SoftwareSerial(2,3);

void setup()
{
GpsMod.begin(4800);
Serial.begin(9600);
}

 void loop()
{
Serial.print(GpsMod.read(), BYTE);
}

In setup function, we initiated software serial communication with GPS module at 4800bps, as it is required on specifications page. We also initiated hardware serial communication with our computer.

In loop function, we are simply bridging data from GPS module, to computer.

After you upload your sketch and open serial monitor, you will see that your GPS module is sending data every one second. Data you are looking at is called NMEA codes.

NMEA reference manual  briefly explains this codes, and can be found here:

http://tronixstuff.files.wordpress.com/2010/09/nmea-reference-manual1.pdf

Also, a nice guide on how to decode NMEA codes is available here:
http://www.gpsinformation.org/dale/nmea.htm#position

It is very straightforward and intuitive.

If you want to play with your GPS Module, you will probably want to go outside. Arduino has internal voltage stabilizer, so you can apply any voltage between 5V and 20V to DC Jack. Recommended voltages are in range between 7 and 12 volts. If you would like to read more about this, you can visit:

http://playground.arduino.cc/Learning/9VBatteryAdapter

http://www.instructables.com/id/Powering-Arduino-with-a-Battery/

Sharp GP2D120 Distance Sensor Tutorial

Introduction

The Sharp GP2D120 is a distance sensor from Sharp’s range of optoelectronic devices. Optoelectronic devices are transducers that work by converting light to electricity or vice versa.

The GP2D120 has two lenses in front; it works by emitting a focused beam of light through one. If there is an object in the sensor’s working range the beam reflects off that object and lands onto the collector (second) lens. This makes a triangle between the emitter, object and collector. The sensor then checks the angle of the reflected beam and uses it to identify how far the object is.
Sensor_diagram

There are two types of inputs digital and analogue. Digital inputs are discrete signals in the form of HIGHs and LOWs. The combination of these HIGHs and LOWs indicates what the data is. Analogue signals are continuous signals in the form of voltage. The GP2D120 distance sensor gives an analogue output. It can detect objects within the range of 40mm to 300mm. When the sensor is turned on it takes 44 ms to start up and has a response time of 39 ms meaning that it updates its values every 39 ms. The average power consumption is 33 mA.

Like everything there are pros and cons for this sensor as well. The Sharp GP2D120 is cheap, light, compact, easy to program and accurate enough for most tasks. On the other hand it can only get a reading of one point of the object, any readings below 40mm are unreliable and there is no possible method of differentiating if the object is right in front of the sensor (0mm) or there is no object in front of the sensor.

//
//

The Tutorial

Requirements

Hardware

  1. Arduino Uno (but any other would work too as long as you can get it to communicate with the PC).
  2. Sharp GP2D120 distance sensor
  3. Arduino IDE

arduino_items

Software

Setting up the Hardware

This tutorial assumes you have an Arduino connected to your computer for programming. Your sensor would have come a cable that consists of three wires; red, black and white. One end of the cable should have a JST female connector and the other end should be three bare wires. Go ahead and connect the JST female connector into your sensor; there is only one right way the cable will go in.

Following are what the three different wires are:

  • Red (VCC) – Connects to the positive terminal of the sensor
  • Black (GND) – Connects to the ground terminal of the sensor
  • White (VO) – Connects to the analogue output from the sensor

Ensure that your Arduino is powered off. Now insert the red wire into the +5V power pin and the white wire into the ground power pin of the Arduino. Insert the white wire into the A0 analogue pin. Plugging in the hardware is now complete. Let’s move on to the programming bit.
arduino_setup

Programming the Arduino

First we define which pin the distance sensor is connected to:

#define ds_pin 0 // Pin to which the distance sensor is connected

TIP: Instead of writing the pin numbers in between the code #define them on top so if you want to check or decide to change the pin to which your device is connected you can quickly do it here.

int ds_value; // Integer to hold the sensor’s reading

The integer ds_value is the variable that we will use to store the sensors value.

To program template for an Arduino requires the following two functions:

void setup ()
{

      // This function runs immediately after the Arduino is turned on   and only runs once
}

void loop ()
{
      // This function keeps looping and is the main function to write               your code in
}

 

Go ahead and initialize the serial port with the following code in the setup loop:

Serial.begin (9600);   // Initiates serial communication at 9600 baud

Note: The arduino can communicate over the serial port with a lot of baud rates (bits per second). You can look these up in Arduino’s reference guide. Do keep this number in mind because when reading the serial port on your PC you will have to set it to that baud rate.

Let’s now enter the first bit of code for our loop function to read the distance sensor values

ds_value = analogRead (ds_pin);  // Reads the distance sensor

This distance sensor outputs an analogue voltage in the range of 0 – 3V (approx.) whereas the analogRead (); command takes the reference voltage to be 5V and gives the output in the range of (0 – 1024) i.e. a value of 5V read using analogRead (); gives the result of 1024. We therefore need to scale our ds_value. Since the maximum value we can get with our sensor is 3 the function will return a maximum value of

,3-5.?1024?615
Therefore:

ds_scaledValue = ((float)ds_value / 615) * 1024;  // Calculates the scaled value

Finally, we output this value to the PC

Serial.println ((int)ds_scaledValue);  // Screams out the scaled value as integer

 

Your final code should look like this:

#define ds_pin 0       // Pin to which the distance sensor is connected
int ds_value;          // Integer to hold the sensor’s reading

float ds_scaledValue;  // Distance sensor’s scaled value

void setup ()
{
  Serial.begin (9600); // Initiates serial communication at 9600 baud
}

void loop ()
{
  ds_value = analogRead (ds_pin);  // Reads the distance sensor
  ds_scaledValue = ((float)ds_value / 615) * 1024; 
  // Calculates the scaled value

  Serial.println ((int)ds_scaledValue);  // Screams out the scaled value as integer
  delay (200);                  // Delay of 200ms
}

Running the Program

Finally! We’re all done. All we need to do now is run the system so go ahead and turn on your Arduino. Ensure that it is connected to the PC. Upload the program to the PC and press the serial monitor icon   run_program    in the top left.

You should now see your Arduino now sending values to you that are the scaled distance sensor values.

Calibrating the Sensor

The datasheet for the sensor will provide you with a graph that shows how the value of the sensor relates the distance between it and the object. These graphs are for an ideal sensor. Every sensor is different (several factors affect the sensors reliability e.g. how old it is, how clean the lens is, how reflective the object is) which is why you will have to calibrate your sensor yourself even if you have multiple new just unboxed sensors.

For example, if you are using the sensor to check if there is something between 50mm and 200mm of the sensor.

#define ds_pin 0    // Pin to which the distance sensor is connected
#define ds_scaledResolution 1024

#define ds_valueAt50 900     // The value the sensor returns at 50mm distance
#define ds_valueAt200 200    // The value the sensor returns at 200mm distance

int ds_value;          // Integer to hold distance sensor’s reading
float ds_scaledValue;  // Distance sensor’s scaled value

void setup ()
{
  Serial.begin (9600); // Initiates serial communication at 9600 baud
}

void loop ()
{
  ds_value = analogRead (ds_pin);     // Reads the distance sensor
  ds_scaledValue = ((float)ds_value / 615) * ds_scaledResolution;  // Calculates the scaled value
  if ((ds_scaledValue <= ds_valueAt50) && (ds_scaledValue >= ds_valueAt200)) // Checks if any object in range
  {
    Serial.println (“Object detected!”);  // Screams if object found
  }
}

 

 

Welcome to Arduino Sensors

Welcome to Arduino Sensors, a website dedicated to all the various sensors and motors for the Arduino board. We will have tutorials to help get you started on your dream project. We will also spotlight some products to enhancement your Arduino.  So welcome and stay awhile.