Showing posts with label GPIO. Show all posts
Showing posts with label GPIO. Show all posts

Monday, December 31, 2012

on Leave a Comment

Interfacing MAX7219 with Raspberry Pi

The MAX7219 is extraordinary IC from Maxim which allows us to control many LEDs utilizing only a couple Raspberry Pi pin-outs. It removes all the hassles related with multiplexing, latching, refreshing common with 7 segments. Just by sending commands  via its SPI interfaceto the MAX7219 can control 64 LEDs or eight 7 segment displays.

Even though Rpi supports hardware SPI I couldn't get it to work with MAX7219. So I abandoned the SPI and connected 3 normal digital GPIO pins to the MAX7219 directly.I connected pin 1 (data), 12 (load) and 13 (clock) to the RPi GPIO pins 17, 23 and 22. Pins 4 and 9 to RPi Ground. Pin 19 to RPi’s 5V along with pin 18 via a resistor of your choice depending on your LED.



















Now it was only a matter of sending commands to the MAX7219 via the data in, clock and load line.

For instance, to display the number 4 on digit 1 we would send the command 0×104 (0×01 : use digit 1, 0×04 : display the number 4). Which might look something like this :

Set LOAD to 1.
Send 16 bits command and data (0×104 = 0000000100000100), and for each binary digit we : set CLOCK to 0; send the binary digit, 0 or 1; set CLOCK to 1.
To finish, set LOAD to 0 then set LOAD to 1.

At this point it might be worth mentioning that the MAX7219 can work in two modes, BCD decode on or BCD off. With BCD on we can just send the number we want to appear on the 7 segment display to the MAX7219 and it will display it, as shown above. As well as digits 0 to 9, 10 to 15 correspond to -, E, H, L, P and ‘ ‘ / blank.

With BCD set to off we can control each segment directly. We’d use this mode if we were controlling a bunch of LEDs

bit  -  segment
0  -  g
1  -  f
2  -  e
3  -  d
4  -  c
5  -  b
6  -  a
7  -  dp

So binary 1110111, 0×77, would display a letter A on a 7 segment display.

Below is the code for MAX7219, using Gordon’s excellent wiringPi library to control the GPIO pins :-

/*
 max7219.c
 to compile : gcc max7219.c -o max7219 -lwiringPi
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
// define our pins :
#define DATA        0 // GPIO 17 (WiringPi pin num 0)  header pin 11
#define CLOCK       3 // GPIO 22 (WiringPi pin num 3)   header pin 15
#define LOAD        4 // GPIO 23 (WiringPi pin num 4)   header pin 16
// The Max7219 Registers :
#define DECODE_MODE   0x09                       
#define INTENSITY     0x0a                        
#define SCAN_LIMIT    0x0b                        
#define SHUTDOWN      0x0c                        
#define DISPLAY_TEST  0x0f                         
static void Send16bits (unsigned short output)
{
  unsigned char i;
  for(i=16;i>0;i--) 
  {
    unsigned short mask=1<<(i-1); // calculate bitmask
    digitalWrite(CLOCK,0);  // set clock to 0
    // Send one bit on the data pin
    if (output & mask)   
      digitalWrite(DATA,1);          
    else                              
      digitalWrite(DATA,0);        
    digitalWrite(CLOCK,1);  // set clock to 1 	 
  }
}
// Take a reg numer and data and send to the max7219
static void MAX7219Send(unsigned char reg_number,unsigned char dataout)
{
  digitalWrite(LOAD,1);  // set LOAD 1 to start
  Send16bits((reg_number << 8) + dataout);   // send 16 bits ( reg number + dataout )
  digitalWrite(LOAD,0);  // LOAD 0 to latch
  digitalWrite(LOAD,1);  // set LOAD 1 to finish
}
int main(void)
{
  printf("\n\nRaspberry Pi Max7219 Test using WiringPi\n\n");
  if(wiringPiSetup()==-1) exit(1) ;
  //We need 3 output pins to control the Max7219: Data, Clock and Load
  pinMode(DATA,OUTPUT);  
  pinMode(CLOCK,OUTPUT);
  pinMode(LOAD,OUTPUT);  
  MAX7219Send(SCAN_LIMIT,7);     // set up to scan all eight digits
/* 
 BCD decode mode off : data bits correspond to the segments (A-G and DP) of the seven segment display.
 BCD mode on :  0 to 15 =  0 to 9, -, E, H, L, P, and ' '	
*/
  MAX7219Send(DECODE_MODE,1);   // Set BCD decode mode on
  MAX7219Send(DISPLAY_TEST,0);  // Disable test mode
  MAX7219Send(INTENSITY,1);     // set brightness 0 to 15	
  MAX7219Send(SHUTDOWN,1);      // come out of shutdown mode
  MAX7219Send(1,6); 		 // displays the number 6 on digit 1
  return 0;
}

Tuesday, November 27, 2012

on Leave a Comment

Reading Analog Values From Digital Pins Of Raspberry Pi

Unfortunately all the 17 pins of Raspberry Pi are digital which can either output HIGH or LOW. But by using a simple circuit (poor man's A/D converter) you can measure multiple level of values using a single GPIO pin. It consists of a basic “RC” charging circuit in which a Resistor in placed series with a Capacitor. The voltage across the capacitor rises when voltage is applied across the RC network. Using the formula [t = RC ] where t is the time,R is resistance in ohms,and C is capacitance in Farads and the time taken to register a HIGH on a GPIO pin we can roughly estimate the analog value.

Algorithm:-

Step 1: Set any GPIO pin as an output and set it Low.This ensures that no charge is present in capacitor and both the terminals are at 0V.

Step 2: Now set the GPIO pin as an input.This will starts a flow of current through the resistors and through the capacitor to ground. The voltage across the capacitor starts to rise. The time taken will be proportional to the input.

Step 3: Read the value from GPIO pin and keep incrementing a counter variable until the value is LOW.

Step 4:  At some point the value from GPIO will register a HIGH. When it does return the value of the counter.

Step 5: Set the GPIO pin as an output and repeat the process as required.

Python Implementation:-


#!/usr/local/bin/python
# GPIO   : RPi.GPIO v3.1.0a
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
# Define function to measure charge time
def RC_Analog (Pin):
  counter = 0
  # Discharge capacitor
  GPIO.setup(Pin, GPIO.OUT)
  GPIO.output(Pin, GPIO.LOW)
  time.sleep(0.1)
  GPIO.setup(Pin, GPIO.IN)
  # Count loops until voltage across capacitor reads high on GPIO
  while(GPIO.input(Pin)==GPIO.LOW):
        counter =counter+1
  return counter
# Main program loop
while True:
  print RC_Analog(4) # Measure timing using GPIO4

Example Circuit:-





Note:- The above technique will only work with sensors that act like resistors like photocells, thermistors, flex sensors, force-sensitive resistors, etc.

It cannot be used with sensors that have a pure analog output like IR distance sensors or analog accelerometers.


Source:- Adafruit

Wednesday, November 14, 2012

on Leave a Comment

7 Segment Multiplexing With Raspberry Pi

7 segments can be driven using many different techniques in conjunction with software and hardware. Multiplexing is one of the most popular methods used to drive a 7 segment display when there are limited no of I/O pins.It uses the concept of POV(persistence of vision) where the human brain cannot detect the flickering of display when the refresh rate is very high(~50Hz).In this method the fundamental logic is to enable or disable the segment blocks at a very high speed at  precise time slices.


7 segment multiplexing with raspberry pi
 In case of Raspberry Pi there are limited no of I/O pins, hence we will use multiplexing. WiringPi is perfect for this job as it uses Arduino like code and the code executes at a higher priority. Display used in this example is of common cathode type.Make sure you have the right 7 segment display,else you will end up getting random segments turned on.For pin mapping check the code below:-


Code:-

#include <wiringPi.h>
#include <stdio.h>
#define DISPLAY_BRIGHTNESS  500
#define DIGIT_ON  HIGH
#define DIGIT_OFF  LOW
#define SEGMENT_ON  LOW
#define SEGMENT_OFF HIGH
int SEGMENT_1=7; 
int SEGMENT_2=11;
int SEGMENT_3=13;
int SEGMENT_4=15;
int SEGMENT_A=3; 
int SEGMENT_B=5; 
int SEGMENT_C=18; 
int SEGMENT_D=19; 
int SEGMENT_E=23; 
int SEGMENT_F=24; 
int SEGMENT_G=25; 
void display_number(int num)
{
  pinMode(SEGMENT_1,OUTPUT);
  pinMode(SEGMENT_2,OUTPUT);
  pinMode(SEGMENT_3,OUTPUT);
  pinMode(SEGMENT_4,OUTPUT);
  long start=millis();
  for(int i=4;i>0;i--)
  {
    switch(i)
    {
    case 1:
      digitalWrite(SEGMENT_1,DIGIT_ON);
      break;
    case 2:
      digitalWrite(SEGMENT_2,DIGIT_ON);
      break;
    case 3:
      digitalWrite(SEGMENT_3,DIGIT_ON);
      break;
    case 4:
      digitalWrite(SEGMENT_4,DIGIT_ON);
      break;
    }
    print_number(num%10);
    num/=10;
    delayMicroseconds(DISPLAY_BRIGHTNESS); 
    print_number(10); 
    digitalWrite(SEGMENT_1,DIGIT_OFF);
    digitalWrite(SEGMENT_2,DIGIT_OFF);
    digitalWrite(SEGMENT_3,DIGIT_OFF);
    digitalWrite(SEGMENT_4,DIGIT_OFF);
  }
  while((millis()-start)<10);
}
void print_number(int num)
{
  pinMode(SEGMENT_A,OUTPUT);
  pinMode(SEGMENT_B,OUTPUT);
  pinMode(SEGMENT_C,OUTPUT);
  pinMode(SEGMENT_D,OUTPUT);
  pinMode(SEGMENT_E,OUTPUT);
  pinMode(SEGMENT_F,OUTPUT);
  pinMode(SEGMENT_G,OUTPUT);
  switch(num)
  {
  case 0:
    digitalWrite(SEGMENT_A,SEGMENT_ON);
    digitalWrite(SEGMENT_B,SEGMENT_ON);
    digitalWrite(SEGMENT_C,SEGMENT_ON);
    digitalWrite(SEGMENT_D,SEGMENT_ON);
    digitalWrite(SEGMENT_E,SEGMENT_ON);
    digitalWrite(SEGMENT_F,SEGMENT_ON);
    digitalWrite(SEGMENT_G,SEGMENT_OFF);
    break;
  case 1:
    digitalWrite(SEGMENT_A,SEGMENT_OFF);
    digitalWrite(SEGMENT_B,SEGMENT_ON);
    digitalWrite(SEGMENT_C,SEGMENT_ON);
    digitalWrite(SEGMENT_D,SEGMENT_OFF);
    digitalWrite(SEGMENT_E,SEGMENT_OFF);
    digitalWrite(SEGMENT_F,SEGMENT_OFF);
    digitalWrite(SEGMENT_G,SEGMENT_OFF);
    break;
  case 2:
    digitalWrite(SEGMENT_A,SEGMENT_ON);
    digitalWrite(SEGMENT_B,SEGMENT_ON);
    digitalWrite(SEGMENT_C,SEGMENT_OFF);
    digitalWrite(SEGMENT_D,SEGMENT_ON);
    digitalWrite(SEGMENT_E,SEGMENT_ON);
    digitalWrite(SEGMENT_F,SEGMENT_OFF);
    digitalWrite(SEGMENT_G,SEGMENT_ON);
    break;
  case 3:
    digitalWrite(SEGMENT_A,SEGMENT_ON);
    digitalWrite(SEGMENT_B,SEGMENT_ON);
    digitalWrite(SEGMENT_C,SEGMENT_ON);
    digitalWrite(SEGMENT_D,SEGMENT_ON);
    digitalWrite(SEGMENT_E,SEGMENT_OFF);
    digitalWrite(SEGMENT_F,SEGMENT_OFF);
    digitalWrite(SEGMENT_G,SEGMENT_ON);
    break;
  case 4:
    digitalWrite(SEGMENT_A,SEGMENT_OFF);
    digitalWrite(SEGMENT_B,SEGMENT_ON);
    digitalWrite(SEGMENT_C,SEGMENT_ON);
    digitalWrite(SEGMENT_D,SEGMENT_OFF);
    digitalWrite(SEGMENT_E,SEGMENT_OFF);
    digitalWrite(SEGMENT_F,SEGMENT_ON);
    digitalWrite(SEGMENT_G,SEGMENT_ON);
    break;
  case 5:
    digitalWrite(SEGMENT_A,SEGMENT_ON);
    digitalWrite(SEGMENT_B,SEGMENT_OFF);
    digitalWrite(SEGMENT_C,SEGMENT_ON);
    digitalWrite(SEGMENT_D,SEGMENT_ON);
    digitalWrite(SEGMENT_E,SEGMENT_OFF);
    digitalWrite(SEGMENT_F,SEGMENT_ON);
    digitalWrite(SEGMENT_G,SEGMENT_ON);
    break;
  case 6:
    digitalWrite(SEGMENT_A,SEGMENT_ON);
    digitalWrite(SEGMENT_B,SEGMENT_OFF);
    digitalWrite(SEGMENT_C,SEGMENT_ON);
    digitalWrite(SEGMENT_D,SEGMENT_ON);
    digitalWrite(SEGMENT_E,SEGMENT_ON);
    digitalWrite(SEGMENT_F,SEGMENT_ON);
    digitalWrite(SEGMENT_G,SEGMENT_ON);
    break;
  case 7:
    digitalWrite(SEGMENT_A,SEGMENT_ON);
    digitalWrite(SEGMENT_B,SEGMENT_ON);
    digitalWrite(SEGMENT_C,SEGMENT_ON);
    digitalWrite(SEGMENT_D,SEGMENT_OFF);
    digitalWrite(SEGMENT_E,SEGMENT_OFF);
    digitalWrite(SEGMENT_F,SEGMENT_OFF);
    digitalWrite(SEGMENT_G,SEGMENT_OFF);
    break;
  case 8:
    digitalWrite(SEGMENT_A,SEGMENT_ON);
    digitalWrite(SEGMENT_B,SEGMENT_ON);
    digitalWrite(SEGMENT_C,SEGMENT_ON);
    digitalWrite(SEGMENT_D,SEGMENT_ON);
    digitalWrite(SEGMENT_E,SEGMENT_ON);
    digitalWrite(SEGMENT_F,SEGMENT_ON);
    digitalWrite(SEGMENT_G,SEGMENT_ON);
    break;
  case 9:
    digitalWrite(SEGMENT_A,SEGMENT_ON);
    digitalWrite(SEGMENT_B,SEGMENT_ON);
    digitalWrite(SEGMENT_C,SEGMENT_ON);
    digitalWrite(SEGMENT_D,SEGMENT_ON);
    digitalWrite(SEGMENT_E,SEGMENT_OFF);
    digitalWrite(SEGMENT_F,SEGMENT_ON);
    digitalWrite(SEGMENT_G,SEGMENT_ON);
    break;
  case 10:
    digitalWrite(SEGMENT_A,SEGMENT_OFF);
    digitalWrite(SEGMENT_B,SEGMENT_OFF);
    digitalWrite(SEGMENT_C,SEGMENT_OFF);
    digitalWrite(SEGMENT_D,SEGMENT_OFF);
    digitalWrite(SEGMENT_E,SEGMENT_OFF);
    digitalWrite(SEGMENT_F,SEGMENT_OFF);
    digitalWrite(SEGMENT_G,SEGMENT_OFF);
    break;
  }
}
int main(void)
{
 printf("7 Segment Multiplexing using Raspberry Pi\n") ;
 if(getuid()!=0) //wiringPi requires root privileges  
 {  
  printf("Error:wiringPi must be run as root.\n");  
  return 1;  
 }  
 if(wiringPiSetup()==-1)  
 {  
  printf("Error:wiringPi setup failed!\n");  
  return 1;  
 }
 int counter=0;
 for(;;)
 {
  display_number(counter++);
                delay(1000);
  if(counter>9999)
   counter=0;
 }
  return 0;
}

Compile the code as...
gcc -o display segment.c -L/usr/local/lib -lwiringPi 
 Now execute it...
 sudo ./display
















Wednesday, September 5, 2012

on 7 comments

Using GPIO on Raspberry Pi to blink an LED

One of the few things that separates the Pi from other SBC (Single Board Computer)  is the ability to use the GPIO (General Purpose Input/Output) pins which can be set as HIGH or LOW to control any external devices. All you need is a female to male jumper wire to get started. Here I have used a HDD IDE connector to get the job done.

In this post pin 9 is used for GND and pin 11 for GPIO17. The LED was connected using a 470 ohm register in series with pin 9 and 11 to limit the current.

GPIO of raspberry pi
Software Implementation:-

The fastest way to get started is to use python which comes pre-installed with all images. Download the RPi.GPIO library and copy the gz tar ball to the RPi wheezy raspbian. Open the terminal and navigate to the extracted folder containing the RPi.GPIO library. Then type:  $ sudo python setup.py install to install the module. Imp: As the OS is multitasking  and not Real-time unlike Arduino there may be jitters depending on CPU priority.

Based on the library I have written a simple code to turn ON and turn OFF the LED after a delay of 1 sec (1000ms) each.The LED blinks 50 times.
import RPi.GPIO as GPIO
import time
# blinking function
def blink(pin):
        GPIO.output(pin,GPIO.HIGH)
        time.sleep(1)
        GPIO.output(pin,GPIO.LOW)
        time.sleep(1)
        return
# to use Raspberry Pi board pin numbers
GPIO.setmode(GPIO.BOARD)
# set up GPIO output channel
GPIO.setup(11, GPIO.OUT)
# blink GPIO17 50 times
for i in range(0,50):
        blink(11)
GPIO.cleanup() 

led blinking with raspberry pi