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

Advanced Arduino Sketch Example: SPI and I2C

You can obviously do great things with blinking LEDs and a console output, but I think you can do much greater things when you add in some more advanced features, like I2C and SPI. RasPiArduino framework support those as well, so we can connect our Raspberry to many different sensors using the I2C bus, or to things like communication modules using the SPI bus. For the I2C device, I will use BH1745NUC color sensor from the ROHM Sensor Evaluation Kit and for the SPI device, I will use SX1278 LoRa module. Both of them run on 3.3V logic, so interfacing them with Raspberry is as easy as connecting a few wires.

Figure 12. Wiring diagram for this example

Figure 12. Wiring diagram for this example

Thanks to LoRaLib and RohmMultiSensor libraries, the code is very simple too! It’s basically the LoRaLib receiver example code combined with BH1745NUC example from RohmMultiSensor library.

// define the sensor we will use
#define INCLUDE_BH1745NUC

// include the libraries
#include <RohmMultiSensor.h>
#include <LoRaLib.h>

// instantiate the sensor's class
BH1745NUC sensorColor;

// SX1278 digital I/O pin 0
int dio0 = 17;

// SX1278 digital I/O pin 1
int dio1 = 27;

// SX1278 SPI slave select pin
int nss = 22;

// create LoRa class instance
LoRa lora(CH_SX1278, nss, BW_125_00_KHZ, SF_9, CR_4_7, dio0, dio1);

// create empty instance of Packet class
Packet pack;

void setup() {
// start I2C communication
Wire.begin();

// initialize LoRa
uint8_t state = lora.begin();
if(state == ERR_NONE) {
Console.println("[SX1278]\tInitialization done.");
} else {
Console.print("[SX1278]\tInitialization failed, code 0x");
Console.println(state, HEX);
while(true);
}

// initialize the sensor with default settings
state = sensorColor.init();
if(state == 0) {
Console.println("[BH1745]\tInitialization done.");
} else {
Console.print("[BH1745]\tInitialization failed, code 0x");
Console.println(state, HEX);
while(true);
}

Console.println("-------------------------------------------------------------------");
}

void loop() {
Console.print("[SX1278]\tWaiting for incoming transmission ... ");

// wait for single packet
uint8_t state = lora.receive(pack);

if(state == ERR_NONE) {
// packet was suceesfully received
Console.println("success!");

// print the data of the packet
Console.print("[SX1278]\tData:\t\t");
Console.println(pack.data);

} else if(state == ERR_RX_TIMEOUT) {
// timeout occured while waiting for a packet
Console.println("timeout!");

} else if(state == ERR_CRC_MISMATCH) {
// packet was received, but is malformed
Console.println("CRC error!");

}

Console.println("-------------------------------------------------------------------");

Console.print("[BH1745]\tColor Values (R-G-B-C):\t");

// measure the sensor values
sensorColor.measure();

// print the values to the console
Console.print(sensorColor.red);
Console.print('\t');
Console.print(sensorColor.green);
Console.print('\t');
Console.print(sensorColor.blue);
Console.print('\t');
Console.println(sensorColor.clear);

Console.println("-------------------------------------------------------------------");
}

This is what the circuit looks like. It’s not pretty, but it gets the job done.

Figure 13. Raspberry Pi connected to SX1278 wireless module and BH1274NUC color sensor

Figure 13. Raspberry Pi connected to SX1278 wireless module and BH1274NUC color sensor.

In this example, all the important information are printed to the Raspberry console. We can see SX1278 successfully received a packet that contains the string “Hello Raspberry!” (sent from an Arduino via LoRenz shield) and the color sensor is measuring correct values!

Figure 14. Raspberry Pi console output

Figure 14. Raspberry Pi console output.

Conclusion

So, here’s the conclusion: can we program Raspberry Pi as an Arduino board? Absolutely! However, there are pros and cons to both systems. Raspberry can process much faster; Arduino is much less power-hungry. Raspberry has built-in HDMI and Ethernet ports, whereas Arduino has built-in analog-to-digital converters. This list could go on, but it all comes down to the fact that Arduino and Raspberry are meant for different things. For example, if you’re building battery-based system for sensor monitoring, go for Arduino. If you’re processing camera pictures with machine learning, then Raspberry Pi would be your choice. The moral of the story is that you should always think about the type of task your system has to handle, and select the components appropriately, not the other way round.

Related Articles

If you’re interested in projects involving both Raspberry Pi and Arduino, check out some of the articles below!

  1. Connecting a Raspberry Pi to an Arduino Uno
  2. Building a Wavetable Synthesizer with a Raspberry Pi and an Arduino Uno
  3. Using I2C and an Arduino Uno to Work With Analogue Voltages on a Raspberry Pi

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