Raspberry Pi – Arduino SPI Communication: FC-28 Soil Moisture Sensor with Raspberry Pi

arduino Raspberry spi - steinacoz

In my last post, I interfaced FC-28 Soil mositure sensor to an arduino board. It worked perfectly, and now I will be incorporated it to Raspberry Pi. Raspberry Pi is the most talked about device now in tech world. I believe you have gone through my earlier post on Soil moisture sensor with ArduinoSerial Peripheral Interface (SPI) is an synchronous serial bus commonly used to send data between micro controllers and small peripherals such as shift registers, sensors, and SD cards.


See also:


Without wasting much time, lets dive into the materials we will be needing for this tutorial. Remember, all of them are available in our store:

As you can noticed, the only new component is the raspberry pi module. We will need to setup the Pi first and make some installations. This posts No Keyboard, Ethernet & Monitor: Setting up Raspberry Pi in headless Mode and Remote Desktop Access setup with xrdp on Raspberry Pi raspbian stretch should be enough to set you up. Don’t forget to enable SPI and I2C communication on your PI. Run:

raspig config
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install python-setuptools
sudo easy_install pip
sudo pip install twython


We need to register a twitter app inorder to get our access keys and tokens. You can get that from HERE, make sure you change the permission to read and write:

Now create a folder and a python file:

mkdir soilmosituretweet
cd soilmosituretweet
sudo nano soilmoisture.py

Add these lines of codes and save, then exit:



#!/usr/bin/env python
import sys
from twython import Twython
import os
import datetime
import csv
import spidev
import time
from time import sleep
import RPi.GPIO as GPIO
import serial


threshold_value = 50

GPIO.setup(sensorpin, GPIO.IN)

CONSUMER_KEY = '********consumer key****************'
CONSUMER_SECRET = '****************consumer secret************'
ACCESS_KEY = '************access key********************'
ACCESS_SECRET = '******************access secret******************'


while True:
    ser = serial.Serial('/dev/ttyACM0',9600) #read from active usb port
    arduino_sensor_value = int(read_serial) //convert to strin
    print (arduino_sensor_value)
    i = datetime.datetime.now()

    #Get the time and date
    timestamp = i.strftime('%H:%M:%S')
    date = i.strftime('%Y/%m/%d %H:%M:%S')
    if arduino_sensor_value < threshold_value:
        #Send tweet
         api.update_status(status='@steinacoz it is '+timestamp+', and my current moisture level is low: '+str(arduino_sensor_value)+'.') #tweet this
        print ('soil moisture normal')

    sleep(6)     # sleep for 6secs         



Now, lets modify our arduino sketch code abit:

this copy belongs to steinacoz platform/stores: www.steinacoz.com , return credit if you intend to use it
Author: Ugwumsinachi Nnadi - CEO steinacoz Ventures

int sensor_pin = A1; //analog pin to attach sensor pin
int buzzer_pin = 12; //attach buzzer via bc337 base to D12
int led_pin = 13; // attach led to D13 or use LED onboard arduino board on D13

int sensor_value ; //variable to hold sensor value
int sensor_value_threshold = 60; //soil moisture threshold value

void setup() {

  pinMode(buzzer_pin, OUTPUT); //assign buzzer pin as output
  pinMode(led_pin, OUTPUT); //assign LED pin as output


   delay(2000); //delay for 2secs


void loop() {

   sensor_value= analogRead(sensor_pin); //read sensor value from analog pin

   sensor_value = map(sensor_value,550,0,0,100);




   if(sensor_value < sensor_value_threshold) { //if moisture level is less than, set buzzer and LED to HIGH 
    digitalWrite(buzzer_pin, HIGH);
    digitalWrite(led_pin, HIGH);
    digitalWrite(buzzer_pin, LOW);
    digitalWrite(led_pin, LOW);



Remember all you have to do is to attach your Arduino board to the right USB port and watch the magic happen. You can run this command to see if your Arduino is attached successfully:

arduino Raspberry spi - steinacoz
arduino Raspberry spi – steinacoz


For more tutorials like these, follow our social media channels and don’t forget to drop your comments.

3 thoughts on “Raspberry Pi – Arduino SPI Communication: FC-28 Soil Moisture Sensor with Raspberry Pi

      • steinacoz says:

        while arduino sends the soil mositure value to Serial via Serial.println() as shown in the arduino sketch.
        You can read the value in your RPi script using:
        import serial

        ser = serial.Serial(‘/dev/ttyACM0’,9600) #read from active usb port
        arduino_sensor_value = int(read_serial) //convert to strin

Leave a Reply

Your email address will not be published. Required fields are marked *