Arduino Nano with DS18B20 temp sensor.

Raspberry Pi Serial Interface Arduino

by Lewis Loflin

This program will reset Arduino, wait a few seconds, then perform a serial read of Arduino and display result with conversions on a terminal.

The Arduino is programmed to operate as a modified 16-bit TLC548 serial ADC.

The Arduino sketch is arduino_ds18b20X2.ino.

This is used with Raspberry Pi 1 and 2 - note GPIO pins.

Arduino is reset by Raspberry Pi and after a short delay performs a serial read in this case of data from a DS18B20 Centigrade temperature sensor. The conversions to Centigrade and Fahrenheit is performed by Raspberry Pi.

#!/usr/bin/env python
# File
# use with sketch arduino_ds18b20X2.ino
# By Lewis Loflin -

# access to GPIO must be through root
import RPi.GPIO as GPIO
import time

GPIO.setwarnings(False) # disable warnings

# These two functions used for DS18B20 sensor

def degC(val):
	return val * 0.0625

def degF(val):
	return (val * 0.0625) * 9 / 5 + 32

CS = 18
CLK =23
dataBit = 24
Reset = 25

GPIO.setup(CS, GPIO.OUT) # CS to Arduino DP5
GPIO.setup(CLK, GPIO.OUT) # CLK to Arduino DP6
GPIO.setup(dataBit, GPIO.IN) # dataBit to Arduino DP4
GPIO.setup(Reset, GPIO.OUT) # Reset to Arduino Reset

# Setup IO
GPIO.output(CS, 0)
GPIO.output(CLK, 0)
GPIO.output(Reset, 0)

# Reset Arduino to perform whatever reading

def resetArduino():
    GPIO.output(Reset, 1)
    GPIO.output(Reset, 0)

def readArduino():
        temp = 0
        GPIO.output(CS, 1)
        # time.sleep(.002)
        for i in range(0,15): # loop here 15 times
                GPIO.output(CLK, 1)
                temp = temp + GPIO.input(dataBit)
                GPIO.output(CLK, 0)
                temp = temp << 1 # shift 1 bit left
        GPIO.output(CLK, 1)
        temp = temp + GPIO.input(dataBit) #  get bit 16
        GPIO.output(CLK, 0)
        GPIO.output(CS, 0)
        # clk one more time
        GPIO.output(CLK, 1)
        # time.sleep(.002)
        GPIO.output(CLK, 0)
        return temp
# begin demo	

print "Please wait for reading..."

j = readArduino()

print hex(j)
print bin(j)
print j
print degC(j), "deg. C"
print degF(j), "deg. F"


print "Exit now."


YouTube Videos:
MCP4725 12-Bit DAC Interface to Raspberry Pi
ADS1115 4-Channel ADC Uses I2C with Raspberry Pi
Interface I2C LCD to Raspberry Pi in C
Pulse-Width-Modulation with Raspberry Pi
Using Geany Text editor C Programming
Raspberry Pi Blink Demo
MAX6675 Raspberry Pi Demo

Raspberry Pi, Arduino, and Learning Linux
Raspberry PI Arduino Advanced Interface
Tkinter with Raspberry Pi and PCF8591 AD-DA Sensor

Linux Videos

Live Linux Distro for Using Printer Port with Electronics
Using the powerful Rox-Filer system in Linux
Use FEH under Linux for a Wallpaper Setter
How to create Symbolic links in Linux