Template for Programming the Pimoroni Pibrella in C
by JRV31 in Circuits > Raspberry Pi
2587 Views, 20 Favorites, 0 Comments
Template for Programming the Pimoroni Pibrella in C
The Pibrella is a good board for a beginner learning to program the GPIO on a RaspberryPi.
They have libraries on their site for programming it in python or scratch, but nothing for C.
This instructable is a template for programming it in C using the wiringPi library.
The wiringPi library lets you program the RaspberryPi in C in a style similar to an Arduino.
Many thanks to Gordon Henderson for his excellent work writing the wiringPi libraries.
Install WiringPi
Gordon Henderson
WiringPi is a set of libraries for programming the RaspberryPi in C.
Instructions for download, install and use are located at http://www.wiringpi.com/
To install wiringPi follow the instructions on this page: http://wiringpi.com/download-and-install/
Also see: https://projects.drogon.net/
And check out his Pibrella page here: https://projects.drogon.net/pibrella-from-pimoroni/
The Pibrella
The Pibrella has three LEDs, a push button switch, and a piezo buzzer onboard.
There are also four inputs and four output pins available.
All the pinouts are defined for you individually.
The inputs, outputs, and LEDs are also defined in arrays, and the
proper pin modes are set in the setup() function.
And Now for the Code
/******************************************************************** * Template for programming the Pibrella from PiMoRoNi in C. * * Dependencies: The wiringPi C libraries must be installed. * Instructions for download, install and use * are located at wiringpi.com * * Compile with: gcc -o [FILENAME] [FILENAME].c -lwiringPi * * Some of the more advanced features require additional #include * statements and Linking additional libraries. * * Run with: sudo ./[FILENAME] * ********************************************************************/
#include <wiringPi.h>
/****************************************************************** * LEDs ******************************************************************/ int ledR = 2; // Red int ledY = 0; // Yellow int ledG = 7; // Green int led[] = {2,0,7}; // Red, Yellow, Green - Array of LEDs. /******************************************************************* * Outputs - There are four output pins marked E, F, G and H. * There are two female connectors for each output. * The connector closest to the red button is the pin (positive). * The other connection is the ground * Each output pin has a status led which lights when the pin is high. ******************************************************************/ int outE = 3; int outF = 4; int outG = 5; int outH = 6; int out[] = {3,4,5,6}; // E, F, G, H - Array of outputs.
/***************************************************************** * Inputs - There are four input pins marked A, B, C and D. * There are two female connectors for each input. * The pin closest to the Red button is the digital input * and the other is + 3.3 volts. * There is an onboard pull down resistor on each input. * Using a switch to short directly to +3.3 volts is okay. * A resistive sensor (photocell) will switch the pin to high when * when the resistance is less than approximentally 30k. * Each input pin has a status led which lights when the pin is high. *****************************************************************/ int inA = 13; int inB = 11; int inC = 10; int inD = 12; int in[] = {13,11,10,12}; // A, B, C, D - Array of Inputs. // Onboard button int BUTTON = 14; //Button has an onboard pull down resistor. // Onboard buzzer int BUZZER = 1;
/**************************************************************** * setup() function, sets all the pinModes for the Pibrella ****************************************************************/ void setup(void) { int i; // Index for for loops. wiringPiSetup(); // Some WiringPi functions require sudo access. // Use wiringPiSetup(). for(i=0;i<3;i++) pinMode(led[i], OUTPUT); for(i=0;i<4;i++) pinMode(out[i], OUTPUT); for(i=0;i<4;i++) pinMode(in[i], INPUT); pinMode(BUTTON, INPUT); pinMode(BUZZER, OUTPUT); }
/******************************************************************* * loop() function, runs in continuous loop when setup finishes. * * <<< YOUR CODE GOES HERE >>> * *******************************************************************/ void loop(void) { // This is sample code, you can delete it // and write your own loop()function here. // In this example the red LED will light // if input A is shorted to +3.3 volts, if(digitalRead(inA)==HIGH) digitalWrite(ledR, HIGH); else digitalWrite(ledR, LOW); // and the green LED will light if the button is pressed. if(digitalRead(BUTTON)==HIGH) digitalWrite(ledG, HIGH); else digitalWrite(ledG, LOW); }
/******************************************************************* * main() function, required in all programs. *******************************************************************/ int main(void) { setup(); while(1) // Do this forever. { loop(); } return 0; }
Setup and Use
Download the file pibrella.c and place it in your home directory.
Turn off your RaspberryPi, plug int the Pibrella.
Plug the power supply into the Pibrella, then re-boot it.
Edit the pibrella.c file. For a lot of programs all you need to write is the loop() function, everything is already done.
Save your source under a different name.
Compile and run it using the commands documented in the comments.