Ressources for Raspberry PI are generally written for Python but, as a micro computer, other languages can be used. If you know C/C++ ( If you come from the Arduino world, for instance) and don’t want to bother learning another computing language, it is possible to program Raspberry Pi using C/C++. In this tutorial we will see how to run C++ on Raspberry Pi and how to program your Raspberry Pi as an Arduino.
Material
- Raspberry Pi3 (or Zero) + monitor + keyboard
- Jumper cables F/F x2
- LED x1
Geany
Raspbian is delivered with Geany which is a text editor that can be used to write and execute a Python code.
- Open Geany
- Create a new file
- In Documents > Define file type > Programming language > Select C or C++ source file
Copy the following code:
#include <iostream> using namespace std; int main(int argc, char **argv) { cout<<"Hello World" << endl; return 0; }
C++ program must be compiled before being executed. In the menu “Build” select “Build”, or press F9 directly, to compile the code. Then, in “Build” select “Execute”, or press F5, to run the code.
The phrase “Hello World” should be displayed in the terminal of the Raspberry PI.
Program your Raspberry Pi like an Arduino
Install WiringPi
To control the I/O of the Raspberry Pi like the ones of the microcontroller Arduino you can use the library wiringPi.h.
In a terminal, check that the library is installed by typing:
gpio -v
gpio readall
If an error occurs, update your Raspbian installation with the commands:
sudo apt-get update
sudo apt-get upgrade
Then copy the GIT repository:
cd
git clone git://git.drogon.net/wiringPi
And compile the library:
cd ~/wiringPi
./build
WiringPi.h Wiring
The library WiringPi uses the GPIO numeration:
You can find the pinout by typing:
gpio readall
Base code to light a LED with Raspberry Pi
Once the library installed, you can write a program using the same keywords as the Arduino in addition to the main() function and the basic includes.
#include <iostream> #include <wiringPi.h> using namespace std; int ledPin = 29; //Correspond à la pin 40 void setup(){ pinMode(ledPin,OUTPUT); cout<<"Hello World" << endl; } void loop(){ digitalWrite(ledPin,HIGH); delay(100); digitalWrite(ledPin,LOW); delay(100); cout<<"Blink the LED" << endl; } int main(void)//(int argc, char **argv) { if(wiringPiSetup()<0){ cout<<"setup wiring pi failed"<<endl; return 1; } setup(); while(1){ loop(); } return 0; }
To compile the C++ program using the wiringPi library with Geany, in the Build>Set Build Commands, in the case corresponding to Build Command, type : g++ -Wall -o “%e” “%f” -lwiringPi. You can then compile the code using the “Build” button and run it correctly using key F5.
Warning: Arduino libraries , otherwise specified, are not supposed to be compatible with Raspberry Pi. You may have to write your own library.
I see that the LED is connected to ground pi GPIO 39 potential Low (0 volt).
and another to GPIO pin 40 potential High about to blink.
This LED is not require a resistance in serial?
I am asking after writing this program. How to save it?
Could be save the file name example: “blink _ led. pyw”
My last question: How to execute (run) it? Is not “sudo Python blink_led.pyw”?
Hello, it is indeed best to add a resitance between the pin and the led.
This code is written in C++ so you shoud save it as .cxx
You can run it directly from Geany by changing the Build Command and then Build and Run.
From the terminal you need to use the g++ command to compile the code and then the command ./yourfile to run the program.
This program says undefined reference to delay, wiringpisetup, digital write, loop, setup, and pinmode.
This happens when I build but not when I compile.
Hi, did you set the build command correctly?
Build Command : g++ -Wall -o “%e” “%f” -lwiringPi
i was include wiringPi.h in code hello.cpp. But porgram says undifined pinMode, delay and wiringpisetup.
I dont know how to fix that
Did you compiled wiringPi?
i done this program using c language works fine but i have one question ,
how do i run my led blink code without using run terminal that is my raspberry pi should work like microcontroller ?
sry for my bad english !
You need to activate the script at startup using the file rc.local
https://www.aranacorp.com/en/programming-with-raspberry-pi/
Great post! I’m eager to try out the C/C++ programming examples on my Raspberry Pi. Can’t wait to see what other cool projects you have in store for us.
Just tried out the C++ example on my Raspberry Pi and it’s working like a charm! Your instructions were clear and concise, thanks for sharing this excellent tutorial