Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

How to Blink LED using Raspberry Pi -(Part 07/38)

By Ajish Alfred

The Intel and ARM are the two popular processor families widely used in computational device. The Intel processors are mostly used in the Personal Computers whereas the ARM processors are optimized for the embedded system applications. The high end ARM processors can provide embedded system devices with that much computational power as most of the PCs have. The Raspberrypi is a low cost minicomputer board designed to be used as a PC for providing computer education for remote schools, where costly desktops are not available. The embedded system people are interested in it since it is using ARM11 processor based microcontroller and unlike the motherboard of the PC, the Raspberry pi board provides lot of pin outs like GPIO pins, serial communication pins etc. which enable them to be used in embedded system applications.

The Raspberrypi board use Broadcom controller chip which is a SoC (System on Chip). This controller has all the peripherals like timers, interrupt controller, GPIO, USB, PCM / I2S, DMA controller, I2C master, I2C / SPI slave, SPI0, SPI1, SPI2, PWM, UART0 and UART1. This SoC has the powerful ARM11 processor which runs on 700 MHz at its core. The controller also has a graphical processing unit (GPU) which includes VideoCore, MPEG-2 and MPEG-4. It also has a 512 MB SDRAM.   

The operating systems like Archlinux ARM, OpenELEC, Pidora, Raspbmc, RISC OS and the Raspbian and also Ubuntu versions are available for the Raspberrypi board. Linux operating systems especially Ubuntu is preferred for all kind of programming and development. This article discusses how to get started with accessing the GPIO pins of the Raspberrypi board using C programming done in Ubuntu.  

[[wysiwyg_imageupload:10535:]]


 

In this project the Raspberrypi board is loaded with Ubuntu and is remotely accessed using VNC. The Raspberrypi board is also connected to the internet. There are 26 connectors which can be taken out from the connector port of the Raspberrypi board. All the connector pins are taken out using 13*2 pin female connectors and at the other end of their wire 26 pin Burg stick male connectors are attached. The Burg stick male connectors allow each pin out from the Raspberrypi board to be plugged into the holes of a breadboard.  

To access the pins that coming out of the Broadcom controller of the Raspberrypi board using C language, a C library is available called “bcm2835”. The first step in the process of C coding for accessing the GPIO of the Raspberrypi board is to install the “bcm2835” library. The bcm2835 library is available to download as a zip file which needs to be copied into the /home/pi folder. The zip file appears like bcm2835-1.26.tar.gz in the folder /home/pi.

The zip file should be unzipped first and then start the installation process. All the process here is done using the Linux command line. The command for unzipping the zip folder to is given below:

tar zxvf bcm2835-1.26.tar.gz

This command will unzip the .gz file into a folder named bcm2835-1.26 in which all the source files are present. Now the user should enter that folder to continue the installation process. To enter the folder use the following command;

cd bcm2835-1.26

Now use the following commands one by one to finish the installation process.

./configure

make

sudo make check

sudo make install

Once the installation is completed the library can be included as a header file in any C codes as

#include <bcm2835.h>

The each pin of the Raspberrypi board is declared inside this header file as their pin number or name etc. The functions for accessing those pins are also available in this header file. The pin out details of the raspberrypi board is given below:

Fig. 2: General Purpose I/O Pins Of Raspberry Pi board

 

PIN NUMBER

DESCRIPTION

1

3.3V

2

5V

3

SDA

4

5V

5

SCL

6

GND

7

SCK, GPIO_7

8

TX

9

GND

10

RX

11

GPIO_0

12

GPIO_1

13

GPIO_2

14

GND

15

GPIO_3

16

GPIO_4

17

3.3V

18

GPIO_5

19

MOSI

20

GND

21

MISO

22

GPIO_6

23

SCLK

24

CS_1

25

GND

26

CS_2

 Fig. 3: Pin Number And Details Of Raspberrypi Board

 

The following section discusses how to write a C code for blinking an LED connected to any of the general purpose output pin of the Raspberrypi, say pin number 11 (GPIO_0).

Few functions from the library <bcm2835.h> can be used here and the details of them are given below:

No.

Function

Description

Parameters

Return

1

int bcm2835_init(void )

initialize the library

 

1 if successful else 0

2

int bcm2835_close(void )

Close the library

 

1 if successful else 0

3

void bcm2835_gpio_fsel

(

 uint8_t pin,

 uint8_t mode

)          

Sets the Function Select register for the given pin, which configures the pin as Input, Output or one of the 6 alternate functions.

pin: GPIO number

mode: Mode to set

 

4

void bcm2835_delay

(

unsigned int millis

)

Delays for the specified number of milliseconds

millis: Delay in milliseconds

 

Fig. 4: Functions Of Library <bcm2835.h> To Blink LED

The functions are used in the C code for blinking the LED which is given in Code 1 Tab.

The user can use vim editor to write the code and save into a file, say “blink.c”. From command line itself use the following command to compile the code with the new library file to generate the executable file “blink”

cc blink.c -lbcm2835 -o blink

The binary file can be executed using the command:

./blink

As soon as the user enters the command the LED connected to the pin number 11 starts blinking. The execution can be terminated using CTRL + C

*******************************************************************************
Note:
In this project the Raspberrypi board version 2 is used, but a previous version of the “bcm2835” library is installed. Accessing the GPIO pin 13 is not possible with this library and hence the 3rd IO pin is selected as pin24 of the P1 port of the Raspberrypi. The circuit and the code are modified accordingly. Those who have the version 2 of the Raspberrypi and the latest version of the “bcm2835” library can use the GPIO pin 13 using “RPI_V2_GPIO_P1_13” instead of “RPI_GPIO_P1_13” in the pin definition.
For example to define a ‘PIN’ as 13th pin of P1 port of Raspberrypi board version2, use the following statement in the code;
#define PIN RPI_V2_GPIO_P1_13

 

*******************************************************************************

Project Source Code

###

 //------------------------------------------ led blinking code --------------------------------------------------//
#include <bcm2835.h>
#define PIN RPI_GPIO_P1_11

int main()
{
if (!bcm2835_init()) // initialize the library
return 1;

bcm2835_gpio_fsel (PIN, BCM2835_GPIO_FSEL_OUTP); // Set the pin to be an output

while (1)
{
bcm2835_gpio_write(PIN, HIGH); // Turn it on
bcm2835_delay(500); // wait a bit
bcm2835_gpio_write(PIN, LOW); // turn it off
bcm2835_delay(500); // wait a bit
}
bcm2835_close(); // close the library

return 0;
}

//------------------------------------------ led blinking code --------------------------------------------------//
 

###

 


Project Source Code

###

#include <bcm2835.h>

// Blinks on RPi Plug P1 pin 11 (which is GPIO pin 17)
#define PIN RPI_GPIO_P1_11

int main()
{

if (!bcm2835_init())
return 1;
// Set the pin to be an output
bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
// Blink
while (1)
{
// Turn it on
bcm2835_gpio_write(PIN, HIGH);

// wait a bit
bcm2835_delay(500);

// turn it off
bcm2835_gpio_write(PIN, LOW);

// wait a bit
bcm2835_delay(500);
}
bcm2835_close();
return 0;
}
 

###

 


Circuit Diagrams

CIRCUIT_51

Project Components

  • LED
  • Resistor

Project Video


Filed Under: Raspberry pi
Tagged With: led, Raspberry Pi
 

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Designing Gate Driver Circuit and Switching Mechanism for Modified Sine Wave Inverter – (Part 9/17)
  • Completing Modified Sine Wave Inverter Design with Full Bridge Circuit and Step Up Transformer – (Part 10/17)
  • Designing an Offline UPS – Part (12 /17)
  • How to reduce Switching Time of a Relay – (Part 15/17)
  • Testing MOSFET – (Part 16/17)
  • Driving High Side MOSFET using Bootstrap Circuitry – (Part 17/17)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • MicroPython – I2C protocol in ESP8266 and ESP3
  • New automotive radar sensor enables reliable in-cabin monitoring system
  • TI breaks ground on 300-mm semiconductor wafer-fabrication plants in Texas
  • New wireless AR Smart Viewer reference design
  • Infineon launches scalable and wireless charging platform with configurable controllers

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd ldr led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • What's the deal with all these "MPPT" IC's with no current sense?
  • Photovoltaic MOSFET Drivers - Voltage Rating
  • Impedance requirement for SDRAM signals
  • A circuit that can adjust a resistance and probing a voltage node
  • A analogue circuit that spit out the resistance desired

RSS Electro-Tech-Online.com Discussions

  • IRS2453 the H circuit
  • Ampro 16mm Stylist projector woes.
  • How to quickly estimate lead acid battery capacity ?
  • Finally switched to Linux.
  • Multistage BJT amplifier
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering