Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
Erik Lee
Published © GPL3+

Realtime Temp Readout via I2C on an Itron Riva Edge Board

This is a great little project to learn I2C hardware communication, bash scripting, and binary manipulation on an Itron Riva Edge board.

BeginnerFull instructions provided3 hours1,400
Realtime Temp Readout via I2C on an Itron Riva Edge Board

Things used in this project

Hardware components

Itron Riva Edge
Itron Riva Edge
×1
Adafruit HT16K33 Backpack
http://www.amazon.com/Adafruit-4-digit-7-segment-Display-Backpack/dp/B00GJRQUTS/ref=sr_1_7?ie=UTF8&qid=1462982065&sr=8-7&keywords=i2c
×1
Adafruit MCP9808
×1
Jumper wires (generic)
Jumper wires (generic)
Required to wire the temps sensor and 7 segment display on the same I2C bus
×1
Breadboard (generic)
Breadboard (generic)
Required to wire the temps sensor and 7 segment display on the same I2C bus
×1
Serial to USB Cable
×1
AC Wall Adapter
×1

Software apps and online services

bash
For shell scripting - comes with Debian
Linux
I used Debian 8
i2c-tools
apt-get install i2c-tools
Microsoft MS Calculator
Hex <-> decimal conversions

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder pins to the 7 segment display and temperature sensor

Story

Read more

Code

temp.sh

Plain text
#!/bin/bash
# Read MCP9808 temp register value
tempRegVal=$(i2cget -y 2 0x18 0x05 w)

### Mask temp value bits (MSB bits 0-3, LSB bits 4-7 for whole numbers, LSB bit 0-3 fractional values)
### LSB bits 8-15, MSB bits 0-7

bit15=$(((tempRegVal & 0x8000)>0))
bit14=$(((tempRegVal & 0x4000)>0))
bit13=$(((tempRegVal & 0x2000)>0))
bit12=$(((tempRegVal & 0x1000)>0))
bit11=$(((tempRegVal & 0x0800)>0))
bit10=$(((tempRegVal & 0x0400)>0))
bit9=$(((tempRegVal & 0x0200)>0))
bit8=$(((tempRegVal & 0x0100)>0))
bit7=$(((tempRegVal & 0x0080)>0))
bit6=$(((tempRegVal & 0x0040)>0))
bit5=$(((tempRegVal & 0x0020)>0))
bit4=$(((tempRegVal & 0x0010)>0))
bit3=$(((tempRegVal & 0x0008)>0))
bit2=$(((tempRegVal & 0x0004)>0))
bit1=$(((tempRegVal & 0x0002)>0))
bit0=$(((tempRegVal & 0x0001)>0))

### Test print to validate the masking
#echo $tempRegVal
#echo $bit15$bit14$bit13$bit12$bit11$bit10$bit9$bit8 $bit7$bit6$bit5$bit4$bit3$bit2$bit1$bit0

### Calc whole number temperature
tempCWhole=$(($bit3*128 + $bit2*64 + $bit1*32 + $bit0*16 + $bit15*8 + $bit14*4 + $bit13*2 + $bit12))

### BASH cannot do floating point match; awk & bc can (bc not installed on Riva boards)
tb11=$(echo - | awk -v var=$bit11 'BEGIN {print var / 2 }')
tb10=$(echo - | awk -v var=$bit10 'BEGIN {print var / 4 }')
tb9=$(echo - | awk -v var=$bit9 'BEGIN {print var / 8 }')
tb8=$(echo - | awk -v var=$bit8 'BEGIN {print var / 16 }')
frSum=$(echo - | awk 'BEGIN {print "'"$tb11"'" + "'"$tb10"'" + "'"$tb9"'" + "'"$tb8"'" }')

### Add whole & fractioanl values together
tempC=$(echo - | awk 'BEGIN {print "'"$tempCWhole"'" + "'"$frSum"'" }')
#echo $tempC

### Convert to F
tempF=$(echo - | awk 'BEGIN {print "'"$tempC"'" * 1.8 + 32 }')
#echo $tempF

### Round to 2 decimal place
tempFRnd=$(echo -| awk -v var=$tempF 'BEGIN { OFMT="%.2f";print  0+var}')
echo $tempFRnd

write7seg.sh

Plain text
#!/bin/bash

### Input params [digit (0-3), value (0-9), period(0/1)]

digit=$1
value=$2
period=$3

if [ -z "$3" ]; then
	period="0"
fi

#echo "Digit: " $digit "Value: " $value "Period: " $period

digitAddr=0x00

case $digit in
	 0) digitAddr=0x08
	 ;;
	 1) digitAddr=0x06
	 ;;
	 2) digitAddr=0x02
	 ;;
	 3) digitAddr=0x00  
esac

digitHex=0x00

case $value in
	0) digitHex=0x3f
	;;
	1) digitHex=0x06
	;;
	2) digitHex=0x5b
	;;
	3) digitHex=0x4f
	;;
	4) digitHex=0x66
	;;
	5) digitHex=0x6d
	;;
	6) digitHex=0x7c
	;;
	7) digitHex=0x07
	;;
	8) digitHex=0x7f
	;;
	9) digitHex=0x67
	;;
	*) digitHex=0x00
esac

### add 128 (0x80) for period
if [ "$period" == "1" ]; then 
	digitHex=$((digitHex + 0x80))
fi

i2cset -y 2 0x70 $digitAddr $digitHex  

tempTo7Seg.sh

Plain text
#!/bin/bash
while :
do

	##Read temp
	temp=$(bash temp.sh)
	echo $temp
	d0=${temp:4:1}
	d1=${temp:3:1}
	d2=${temp:2:1}
	d3=${temp:1:1}
	d4=${temp:0:1}
	
	### for temps < 100, using 1 decimal place:
	bash write7seg.sh 3 $d4 0
	bash write7seg.sh 2 $d3 1
	bash write7seg.sh 1 $d1 0
	bash write7seg.sh 0 $d0 0 
	
	sleep 1
done 

Credits

Erik Lee
1 project • 2 followers
Contact
Thanks to Norm McEntire.

Comments

Please log in or sign up to comment.