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

Wednesday, July 22, 2015

ARTICLE #1 - Using an 8-bit shift register with Arduino



What is a shift register?

74HC595 8-bit shift register
Did you ever wanted to control so many outputs (ex: LEDs, motors, servos …) in a project you work on, then you realized that your microcontroller does not have enough pins to match the requirements of your project? Say, for example, you want to control 16 LEDs but you looked at your Arduino Uno board and found that it has just 14 I/O pins in total. That’s where the shift register comes handy for you; this device allows you to extend the number of pins on your Arduino Board. So, in the previous situation of you wanting to control 16 LEDs, it turns out that you’ll need just 3 pins to do it! 

In this tutorial I’ll be using the 74HC595 8-bit shift register; it is a Serial-In-Parallel-Out (SIPO) ship that provides the microcontroller with a total of 8 extra outputs to use.



How does it work?

        Features and benefits :

  • Supply Voltage Range from 2.0V to 6.0V
  • CMOS low power consumption
  • Storage register with 3-state outputs
  • Schmitt Trigger Action at All Inputs
  • Inputs accept up to 6.0V
  • 100 MHz (typical) shift out frequency
  • Specified working temperature from -40 °C to +125 °C
You can find more features and specifications of the 74HC595 shift register here : datasheet

       Pinning information:

The table below shows pin configuration of the 74HC595 shift register:



The 3 pins that control the Output pins are SH_CP, ST_CP and DS.

The process starts like this:

a logic level of “1” or “0” comes in to the serial data input (DS).

At the rising edge of the shift register clock pin (SH_CP), data gets shifted from pin DS to pin Q0.

At the next rising edge of SH_CP, the Q0 value shifts to Q1 and the new DS value to Q0.

Also at the next rising edge of SH_CP, data present at Q1 shifts to Q2, Q0 value to Q1, and the new DS value to Q0.

And so on and so forth, values in the output pins shift to the next pin at every rising edge of SH_CP.

This process of shifting values occurs internally, so we have to wait for the rising edge of the Storage Register Clock Pin (ST_CP) to set the output pins to the new shift register values.




Wiring:


The project needs 3 main components: an Arduino UNO, a 74HC595 shift register and a strip of LEDs.

First you have to connect the pin 8 of the shift register to the Ground, and pin 16 to the 5V supply voltage on your Arduino UNO board.

Then you connect each LED to an output pin of the shift register starting from Q0 to Q7.

Both pins 10 and 13, Master Reclear and Output Enable are active low, which means they have to be pulled down to GND in order for them to be activated.

Pin 9, 11 and 12 are connected to the digital I/O pins of the Arduino board. They are the pins that we use in the program code to control the shift register (as you will see in the code part of this tutorial). You can connect them to any digital I/O pin of your microcontroller.

Pin 9 is used to connect the 8-bit shift register to another 8-bit shift register to chain them together to make a 16-bit shift register; you can connect as many shift registers as you want, so you have just to connect this output pin to the Q0 pin of the next shift register. Because in this project I am using only a single 8-bit shift register, the pin 9 is connected to nothing. 






Code:


This code example below just turns on the strip of LEDs back and forth, lighting up one LED at a time in a Pingpong-like pattern.

I will comment every section of the code in order to make it easier to understand.

//The 3 output pins of the 74HC595 shift register are connected to 3 digital I/O pins of the microcontroller
int DS_pin = 13;
int STCP_pin = 11;
int SHCP_pin = 10;

void setup()
{
// All the 3 digital pins are initialized as outputs
           pinMode(DS_pin,OUTPUT);
           pinMode(STCP_pin,OUTPUT);
           pinMode(SHCP_pin,OUTPUT);
}

unsigned int registers[8] = {1,2,4,8,16,32,64,128};
// Every element of the table represents
                                                                                 // a turned-on LED
                                // The elements of the table are the integer conversion of their binary values
                                // Ex : 32 àb: 00100000 àLED number 6 is on

unsigned int LED; // This variable stores the value of the current Led on 

// this function connects every value of the previous table to its output correspondent on the shift register 
void writeregister()
{
         digitalWrite(STCP_pin, LOW);
         shiftOut(DS_pin, SHCP_pin, LSBFIRST, LED);
         digitalWrite(STCP_pin, HIGH);

}

void loop()
{
         //The loop for turning on LEDs forward
         for(int i = 0; i<7; i++)
         {
                  LED = registers[i];
                  delay(300);
                  writeregister();
         }

        
//The loop for turning on LEDs backward
        for(int i = 7; i>0; i--)
        {
                 LED = registers[i];
                 delay(300);
                 writeregister();

        }
}

Monday, July 20, 2015

Hello world!

Hello, on this blog, I will share with you some Arduino tutorials and basic electronic systems.