logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Raspberry Pi

How to Run Arduino Sketches on Raspberry Pi

DevicePlus Editorial Team
Published by DevicePlus Editorial Team at June 30, 2021
Categories
  • Raspberry Pi
Tags
  • Arduino
  • python
  • raspberry pi
  • raspiarduino
  • sketches
How to Run Arduino Sketches on Raspberry Pi

Basic Arduino Sketch Example: LED and Console

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

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

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

Figure 11. Raspberry Pi console output

 

1 2 3 4
DevicePlus Editorial Team
DevicePlus Editorial Team

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • Guide to Connecting Arduino & Raspberry Pi + DIY Arduino and Raspberry Pi Camera Robot!Guide to Connecting Arduino & Raspberry Pi + DIY Arduino and Raspberry Pi Camera Robot!
  • How to Add Siri Control to Your Raspberry Pi ProjectHow to Add Siri Control to Your Raspberry Pi Project
  • Arduino Preprocessor Directives TutorialArduino Preprocessor Directives Tutorial
  • The Sense HAT Add-On Board For Raspberry Pi – JoystickThe Sense HAT Add-On Board For Raspberry Pi – Joystick
  • Raspberry Pi Camera: Mini Fixed Point Camera Using Raspberry Pi Zero V1.3 & Camera ModuleRaspberry Pi Camera: Mini Fixed Point Camera Using Raspberry Pi Zero V1.3 & Camera Module
  • The Sense HAT Add-On Board For Raspberry Pi – Operating The LED DisplayThe Sense HAT Add-On Board For Raspberry Pi – Operating The LED Display
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort