Enough with the theory, time for some practice! We’ll start easy, with the most basic Arduino functions: LED and Serial output. We will use the following Arduino sketch, which is a slightly modified “Fading” Arduino example. The LED brightness is set using PWM (Pulse Width Modulation) and the current PWM value is printed to the console.
// GPIO pin with PWM support int pwmLedPin = 12; // GPIO pin without PWM support int onOffLedPin = 6; void setup() { // set both pins to output pinMode(pwmLedPin, OUTPUT); pinMode(onOffLedPin, OUTPUT); } void loop() { // turn the first LED fully on digitalWrite(onOffLedPin, HIGH); // fade in the second LED for(int pwm = 0; pwm <= 255; pwm += 5) { // set the PWM value analogWrite(pwmLedPin, pwm); // print the PWM value to console Console.println(pwm); delay(30); } // turn the first LED fully off digitalWrite(onOffLedPin, LOW); // fade out the second LED for(int pwm = 255; pwm >= 0; pwm -= 5) { // set the PWM value analogWrite(pwmLedPin, pwm); // print the PWM value to console Console.println(pwm); delay(30); } }
We have the code, now we need to wire all the components together. Nothing complicated here either, just two LEDs and 10k resistors.
Figure 9. Wiring diagram for this example
The final step in programming Arduino boards is to press the magic “Upload” button and watch the sketch being compiled and uploaded into the hardware. With RasPiArduino framework, there are two options how to upload the sketch to the Raspberry.
1. Using the “Upload” button. Connect the Raspberry to your local network, and its IP address should be listed in the “Ports” section of “Tools” menu. Select it, just like you would select an Arduino COM port, and press “Upload”. Then, you will be prompted to enter the root password. Once you enter it, the sketch will upload and automatically execute. You can view the Console output in the Arduino serial monitor.
2. Uploading manually. In Arduino IDE, go to “Sketch” menu and press “Export compiled binary”. Your sketch will now be compiled and the binary will be exported into .bin file in your sketch folder. You now have to copy this file to your Raspberry Pi. The next step is to make the binary executable; this can be achieved with the following command:
chmod +x exported_binary.bin
You can now run your binary by another command, which has to be executed as root:
sudo ./exported_binary.bin
All Console output will now appear in the console. The program can be stopped just like any other terminal program by the keyboard shortcut Ctrl+C. I prefer manual upload, because it doesn’t require additional services to be set up, and because it is generally more reliable (at least with my setup).
Upload the sketch using one of the two above methods and watch one of the LEDs blink on and off, while the other gently fades in and out!
Figure 10. Raspberry Pi controlling two LEDs
The sketch also prints the current PWM value into console, and this is what the output looks like.
Figure 11. Raspberry Pi console output