Monday, August 24, 2015

ARTICLE #3 - Arduino SPI communication using 74HC595 8-bit shift register


In this tutorial we are going to start learning about a great protocol used for implementing communication between microcontrollers and their peripheral devices on a serial bus. This protocol is generally used for its high speed data transfer rate than asynchronous protocol communications such as CAN and UART. I guess the question that comes directly to your mind is: what is an SPI protocol ? 


SPI protocol :



Serial peripheral interface, or SPI, is a synchronous serial communication protocol used by microcontrollers to communicate with one or more peripheral devices. The main difference between the SPI protocol and the old asynchronous communication protocol (ex: CAN bus) is that it has a clock signal that synchronizes the communication between devices. SPI protocol is used as a standard communication for  a variety of peripheral devices such as sensors, EEPROM memory, SD cards, etc.


Pin description :




In SPI communications, there is only one device that generates the clock speed which is usually a microcontroller (the Arduino board in this tutorial), it is called a Master. The other devices have to set up the same clock speed and they are called Slaves.



SPI bus: single master and single slave



  • MOSI : Master Out Slave In is the line used to send data from Arduino to the peripheral device.
  • MISO : Master In Slave Out is the line used to send from the peripheral devices to Arduino.
  • SCK : This line is used to transmit the clock signal generated by the Master device (Arduino) to the Slave device’s clock to synchronize data transmission.



When multiple peripheral devices are used, we add another line to select the device which is :



CS : Chip Select is the line that selects the slave device that the master wants to talk to. All CS lines are set to high (logic 1) which disconnects the slave devices from SPI bus. Before sending data, Arduino sets the desired  CS line to low to activate the peripheral device that it wants to communicate with. 






SPI bus: master and three independent slaves




For further information about the SPI protocol, you can visit these links to see more detailed illustrations :

Serial Peripheral Interface — Wikipédia



SPI protocol implementation :



     - SPI library :



It is a standard library which is included by default with the Arduino IDE, it allows you to use multiple functions used for establishing communication. You can add it by going to Sketch -> Import library -> SPI on your Arduino software, or by just adding this line to the beginning of your program :


 #include <SPI.h>    




Now that you included the SPI library into your program, you have to set up some functions to initiate communication :


    - Hardware connexions :




For arduino Uno board, you have to connect the pins as follows :



MOSI : pin 11
MISO : pin 12
SCK : pin 13
/Cs : You can use any digital pin and set it to output.




Example : using SPI with 74HC595 shift registers :



In this example, we are going to set up an SPI communication between the Arduino microcontroller and the 74HC595 via its serial data input DS. If you are not introduced to use shift registers you can check my previous article to see what it is and how it works.



This is a simple program that shows how to implement SPI communication, initialize it in the setup function and then use it to send data to the shift register’s DS input, the data sent is an 8-bit binary counter that increments every time the Chip Select goes low.



 //includes the functions necessary for SPI communication
 #include <SPI.h> 


 const int CS_pin = 10; //Chip Select


 void setup() {

       pinMode(CS_pin, OUTPUT); //CS as output
       SPI.begin(); //starts the SPI communication
       SPI.setBitOrder(MSBFIRST); //data sent MSB first

 }

 void binary_counter(int cnt) {
        
       digitalWrite(CS_pin, LOW); //Chip select low to select the slave device
       SPI.transfer(cnt); //data transfer
       digitalWrite(CS_pin, HIGH); //deselect the device by turning it to high
       cnt++; //counter incrementation
       delay(200);

 }

 void loop() { 
      
      int counter = 0;
      binary_counter( counter); //calls the binary_counter function

 }


Wednesday, August 12, 2015

ARTICLE #2 - TMP36 Temperature Sensor with Arduino



Introduction :



TMP36 sensor

A temperature sensor, as its name indicates, is a chip that is used to measure the temperature of its surrounding environment.

Temperature sensors are used in multiple applications that require accurate temperature measurements such as weather forecasting, food processing, medical devices, also in automotive industry and many other fields.

The sensor that I will be using in this project is the TMP36 sensor from Analog Devices. It's an electrical sensor that, when it is connected to a microcontroller, converts data concerning temperature that it gathers from its surrounding environment to an electrical signal on its output that can be used by the microcontroller in the different computations that it performs.



TMP36 Sensor :



From the datasheet we can read that :

The TMP35/TMP36/TMP37 are low voltage, precision centigrade temperature sensors. They provide a voltage output that is linearly proportional to the Celsius (centigrade) temperature. The TMP35/ TMP36/TMP37 do not require any external calibration to provide typical accuracies of ±1°C at +25°C and ±2°C over the −40°C to +125°C temperature range.

The TMP36 features are :

TMP36 pin-out
  • Low Voltage Operation (+2.7 V to+5.5 V)
  • Calibrated Directly in °C
  • 10 mV/8°C Scale Factor 
  • ±2°C Accuracy OverTemperature 
  • ±0.5°C Linearity 
  • Stable with Large Capacitive Loads
  • Specified -40 °C to +125 °C, Operation to +150 °C
  • Less than 50 µA Quiescent Current
  • Shutdown Current 0.5 µA max


You can find more specifications here : datasheet


How temperature is measured ?

The most relevant characteristic of the TMP36 sensor is the relation between the temperature sensed and the voltage measured on its output pin.


The image below shows the plot of the output voltage as  a function of temperature.




The equation of this linear function is :

               Voltage = 0.01*Temperature + 0.5

The temperature as a function of voltage becomes : 

               Temperature = (Voltage - 0.5)*100  

So, for example if the voltage measured at the TMP36 output pin is 1V that means that the temperature is (1 - 0.5)*100 = 50 °C




Circuit wiring :

Project Parts :

The parts that you will need in this project are the following :
  • 1 breadboard.
  • 1 Arduino Uno.
  • 1 TMP36 sensor.
  • 5 jumper wires.

TMP36 Pin Description:

There are 3 pins on the TMP36 sensor, when seen from the front side they're wired as the following :
  • The left pin is connected to the 5 Volts pin of the Arduino Uno board.
  • The middle pin is the SIGNAL pin, it is connected to one of the Analog Input pins of the microcontroller.
  • The right pin is connected to the ground pin (GND).

Wiring:


The hardware wiring is very simple. The image below shows how the project's parts are wired together, the temperature sensor's pins are connected as described in the TMP36 pin description part.






Code :


This code example below shows how to configure a temperature sensor. The program also sets a serial communication with the computer to display the detected temperature on the serial monitor in the Arduino IDE.


//The SIGNAL pin of the TMP36 sensor named temperaturePin and connected to the analog pin 2  //of the Arduino board
int temperaturePin = 2;

void setup() {
  //This function of the Arduino Serial library starts the serial connection with the          //computer. 9600 is the Baud rate (9600 bits/sec)
  Serial.begin(9600);

}

void loop() {
  //creates the variables that we will be using
  float voltage, degreesC, pinValue;

  //stores the value that it has been read from temperature sensor
  pinValue = analogRead(temperaturePin);

  //Converts the pinValue to voltage
  voltage = pinValue*5/1024;

  //this formula converts the voltage to degrees Celsius
  degreesC = (voltage - 0.5) * 100.0;
  
  //these 2 functions are used to print data to the serial monitor of the computer
  Serial.print("deg C: ");
  Serial.println(degreesC);
  
  delay(1000);

}

After that you execute this program, open the serial monitor in the Arduino IDE.
You should be able to see the current temperature starting to be printed inthe serial monitor every 1 second like this :


deg C: 20.82 deg C: 21.78 deg C: 21.78 deg C: 23.73 deg C: 23.98 deg C: 25.68 deg C: 27.30 deg C: 31.05