logo-mobile

ROHM

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

Raspberry Pi

How to Run Arduino Sketches on Raspberry Pi

Jan Gromes
Published by Jan Gromes at February 21, 2018
Categories
  • Raspberry Pi
Tags
  • Arduino
  • python
  • raspberry pi
  • raspiarduino
  • sketches
arduino code raspberry pi

Advanced 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.

arduino code raspberry pi

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.

arduino code raspberry pi

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!

arduino code raspberry pi

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.

1 2 3 4
Jan Gromes
Jan Gromes
Jan is currently studying Electrical Engineering at Brno University of Technology. He has many years of experience building projects using Arduino and other microcontrollers. His special interest lies in mechanical design of robotic systems.

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • Raspberry Pi WebIOPi IOT Part 3 – Programming Basics (Input/Output)Raspberry Pi WebIOPi IOT Part 3 – Programming Basics (Input/Output)
  • 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!
  • 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
  • How to Add Siri Control to Your Raspberry Pi ProjectHow to Add Siri Control to Your Raspberry Pi Project
  • 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
  • DIY Tip: How to Set Up Your Raspberry PiDIY Tip: How to Set Up Your Raspberry Pi
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate RFID module with Raspberry Pi How to integrate RFID module with Raspberry Pi
  • nRF24L01+ RF Module Tutorial nRF24L01+ RF Module Tutorial
  • 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-2021. Device Plus - Powered by ROHM
© 2021 Device Plus. All Rights Reserved. Muffin group