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
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.
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.
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.
If you’re interested in projects involving both Raspberry Pi and Arduino, check out some of the articles below!