Lesson 1- LED Blinking

 

Introduction

In this lesson, we will be learning how to blink LEDs of BreadPi using Raspberry Pi. We will also see its Scratch, Python and C program. Now, let’s start the learning process.


Components Required:

  1. Raspberry Pi
  2. Breadboard

What is BreadPi

 BreadPi is basically a HAT designed specifically for Raspberry Pi by SB Components. It is designed keeping in mind the common hardware interfacing problems that every beginner and experienced face. It is designed for beginners as well as professionals for hardware testing and development.

BreadPi is a Raspberry Pi HAT to provide added features to the Raspberry Pi. This shield provides digital and analog I/O functionalities with a breadboard to add hardware connections for testing, learning, and training purposes. BreadPi is an easily stackable Raspberry Pi Hat with dimensions of around 65mm x 56 mm. Stacked Breadpi covers the Raspberry pi from the top giving access to all the ports except GPIO header. Almost all the GPIOs can be accessed through the BreadPi.

Although it is very easy to interface any Digital I/O device with Raspberry Pi, for beginners it can be really hard and confusing. Everyone makes mistakes of connecting wrong pins and expecting a write output. All the confusion arises from the 40 pin GPIO header of Raspberry Pi. The BreadPi resolves this problem by grouping similar pins together. There is 1 buzzer, 4 LEDs, and 2 tactical switches on the BreadPi for digital input and output functionalities. These I/O components are mounted on the BreadPi to reduce our effort in making direct connections through jumper wires. 


Features of BreadPi :

  1. 8-bit Analog Read/Write,
  2. 5 and 3v3 tolerant GPIOs,
  3. Breadboard for temporary prototyping,
  4. Raspberry Pi Zero/ Zero W/ Zero WH /2B /3B /3B+ /4B Compatible
  5. Easy GPIO pin access,
  6. Buzzer, LED, and Buttons for I/O operations,
  7. Easy access to power pins,
  8. Dimensions: 65mm × 56mm

Applications :

  1. Analog to Digital Conversion,
  2. Digital to Analog Conversion,
  3. IoT,
  4. Training and Development

Connecting BreadPi to Raspberry Pi :

BreadPi doesn’t need any external power supply. Using BreadPi is very easy, like other HATs. It is stackable over all Raspberry Pi 40 pins models. After stacking you have access to header pins on the top of the board. For using BreadPi, stacking it over the Raspberry Pi and using its features from the program are necessary. Before stacking BreadPi over the Raspberry Pi keep the power unplugged. The female headers of the BreadPi will head over to male headers on the Raspberry Pi. Insert the BreadPi headers into the Raspberry Pi pins, if all the Raspberry Pi pins are covered give it a gentle push to make the connection tight. Now you can power up the Raspberry Pi.

After all the connections are done its time to start programming your BreadPi. The programming can be of your preference. You can code your BreadPi in C and Python but those who are completely new or the newbie coders who have a little knowledge about programming can start with scratch programming. In this lesson, we will be providing the LED blinking program in all 3 programming language- Scratch, Python and C.   

You can find 4 LEDs on the BreadPi which you have to program. LEDs are the output device widely used in displays and indicators. The LEDs L1, L2, L3 and L4 are at pin 32, 36, 38 and 40 of Raspberry Pi respectively. All the LEDs are in series with a 1 kOhm resistor to limit current and prevent it from burning.

 The pin number of the LEDs are given in the following table which will help in the programs:

LED

ONBOARD 

BCM

WIRINGPI

LED 1

32

12

26

LED 2

36

16

27

LED 3

38

20

28

LED 4

40

21

29

 

SCRATCH

Scratch 3.0 is the latest version which we are going to use for programming. The Scratch 3.0 is way different and better from the later versions. Scratch user can definitely identify the differences. Scratch is for the newbies and is a great platform to understand programming logic using drag and drop method.   

In the program, all we have to do is to drag the blocks from the block palette. Also, add the GPIO pins extension. We’ll be writing a program to blink all the 4 LEDs for 2 seconds, you can change the time from adjusting the delay. On clicking the green flag the program will start and on clicking the red button the programs. Drag the forever loop block so the program can run in a loop and inside the forever block add the GPIO block with high and low command. Also, don’t get confused while entering the LED pin numbers its the BCM pin numbering. Add delay of 2 seconds or more time so you can the blinking properly. End the programing by saving the file.


Scratch example program for BreadPi LED blinking 



PYTHON


For Python programming, we have BreadPi library which you can add by writing the following command in your Raspberry Pi terminal:

pip3 install breadpi

or

python3 -m pip install breadpi

The BreadPi package will be installed and you can import it in your program by using:

import BreadPi.

You can use any of the Python editors for programming. You can write codes using the BreadPi library or without using it, it’s totally on your choice whether you want to use the library or not.

 

If you want to use the library then first import the library followed by importing time. The above program is for the blinking of LED randomly. We have used the inbuilt randint function to select LED randomly. Function blink_leds is made for the blinking process. Call the function in the try section otherwise it will go KeyboardInterrupt section and will be passed.

 

Unlike the previous program in this, we have used the RPi pins. This is much simple and easier program and is for the beginners. We have imported the GPIO pins from RPi.GPIO library to get access to all the pins. Define there pin numbers and we are using the onboard numbering which can define in the setmode. Make all the LED pin output by setup. When the program will go into the while loop it will print “LEDs are blinking” during the on state and “LEDs not blinking.” during the off state. 


C Programming


Not only python and scratch you can also do programming of BreadPi in C also. We are using the Geany C editor.   

For the C program, we need WiringPi which is a library written in C used to access GPIO pins on Raspberry for BCM2835 (Broadcom Processor) SoC (System on Chip).

First, add the header files which consists of the C function declaration and macro definitions. Provide the BCM pin number to every LED. Define the main function int main() next, int main() is the line from execution starts and the void in the bracket tells that the main does not take any parameter.  From the curly bracket program starts, the if is to check the WiringPi setup if any error comes the statement prints “ setup wiringPi failed!” and the program terminates from there. After checking the error next is to set the pinMode to Output for every LED. Then comes While() which is an entry Controlled loop in C which is used to execute the codes with given constraints or Conditions. While(1) will run infinitely unless it finds a break statement or gets stopped manually. Inside the while loop, we have given all LEDs high for 2 seconds and low for the next 2 seconds which make all 4 LEDs blinking. Return 0 denotes successful termination. 

Save the file by adding .c in the end- filename.c. Before building the program make some changes in the set built command add -l wiringPi after the Compile Command and also in the build Command. Add sudo before “./%e” in Execute command. Make sure there is a space before -l WringPi and after sudo. Now you can build, compile and execute your program.