0

I have a Raspberry Pi 3, am reasonably new to the RP, and have tried to get the RP talking to an i2c slave device using Node-Red, nodejs, and python, but without success. The device works perfectly when connected to an 8-bit MCU.

The strange thing is, when I run i2cdetect -y 1, the device is detected, yet when I attempt to run i2cget, the same byte value is returned, irrespective of the device register address I attempt to read. The byte value is also equal to the last value I sent to the device using an i2cset command.

If anyone with experience using i2c on the RP can assist, this will be appreciated.

Thank you.

import smbus 
import time 

bus = smbus.SMBus(1) 
addr = 0x67 

def readbyte(): 
    val1 = bus.read_byte_data(addr, 0) 
    return val1 

while True: 
    val2 = readbyte() #this returns the value as a byte between 0 and 255. 
    print val2 
    time.sleep(1)
13
  • ...and the device is? The Python script you used will also be useful.
    – joan
    Oct 26, 2016 at 15:15
  • ..an LT power monitor. As mentioned, it works perfectly with C code. I'm not familiar with python, so I change values in the python code and rerun it. I seem to have an issue formatting the code, here it is anyway import smbus import time bus = smbus.SMBus(1) addr = 0x67 def readbyte(): val1 = bus.read_byte_data(addr, 0) return val1 while True: val2 = readbyte() #this returns the value as a byte between 0 and 255. print val2 time.sleep(1)
    – Ol_Devel
    Oct 26, 2016 at 15:30
  • Edit new information into your question, not the comments.
    – joan
    Oct 26, 2016 at 15:36
  • I believe it should be obvious that that is not what I am attempting to achieve. As mentioned, the C code runs on an 8-bit device I would like to have the option to use node or python on the Raspberry Pi 3.
    – Ol_Devel
    Oct 26, 2016 at 15:40
  • I know this is besides the point but if you're "not familiar" with python why don't you just stick with C? More on point: If you have an example in C that works and one in python that doesn't, it would be good if you posted a minimal version of each. And as joan points out, please edit information into the question; like S.O. we aren't a discussion forum. There are enough people around here with "experience using i2c on the RP" but we probably aren't inclined to drag stuff out of you if you can't be bothered to be more specific in describing the details of your problem.
    – goldilocks
    Oct 26, 2016 at 17:46

3 Answers 3

2

Part 0 : Assumptions & Disclaimers

  • Freshly baked (flashed ;p) microsd card containing the latest raspbian distro (jessie as of this writing)
  • RPI 3
  • kernel version > 3.18 (uname -a should tell what is the running kernel's version)
  • below instructions taken from from adafruit solely for guiding new folks (and my own reference), no malice intended in any way

Part 1 : Setup

  • Installing the required packages

  • python support for smbus

sudo apt-get install python-smbus

  • i2c debugging tools

sudo apt-get install i2c-tools

  • Enabling kernel support (i2c is off by default) using raspi-config

  • raspi-config -> advanced options -> A7 i2c -> arm i2c interface enable (yes)

  • raspi-config -> advanced options -> A7 i2c -> load i2c kernel module by default (yes)
  • reboot

  • Enabling device tree overlay

sudo nano /boot/config.txt

  • Append the following lines

dtparam=i2c1=on dtparam=i2c_arm=on

  • reboot

Part 2 : Verification

  • use i2cdetect to check if the device is detected

sudo i2cdetect -y 1

detected device are indicated with addresses that are not --

Part 3 : Connecting LTC2945

There is a rather long discussion on the raspberrypi forum here on the topic of connecting to LTC2945.

Here is the extract that is probably most relevant to the question at hand

by joan » Sat Dec 19, 2015 10:47 am

munnik wrote: ...

I2C Interface The LTC2945 includes an I2C/SMBus-compatible interface to provide access to the onboard registers. Figure 5 shows a general data transfer format using the I2C bus. The LTC2945 is a read-write slave device and supports the SMBus Read Byte, Write Byte, Read Word and Write Word protocols. The LTC2945 also supports extended Read and Write commands that allow reading or writing more than two bytes of data. When using the Read/Write Word or extended Read and Write commands, the bus master issues an initial register address and the internal register address pointer automatically increments by 1 after each byte of data is read or written. After the register address reaches 31h, it will roll over to 00h and continue incrementing." A Stop condition resets the register address pointer to 00h. The data formats for the above commands are shown in Figures 6 to 11.

So I would guess that my original code in this post should work as expected? Is this a bug in the Linear device, I'm not an I2C expert so I'm not sure.

From memory it's more of a bug in the Pi's I2C software driver.

The problem is the handling of repeated starts.

The driver is sending start address (0x6F) write (5) stop start address (0x6f) read ... stop, it needs to send start address (0x6F) write (5) start read ... stop.

From the Linear datasheet "A Stop condition resets the register address pointer to 00h.". It's that superfluous stop which causes the problem.

The I2C module does have a combined flag which you can try setting. That is meant to permit repeated starts. I have not had reliable results when I tried using it, quite possibly my mistake.

by munnik » Sat Dec 19, 2015 12:09 pm Great! I switched back to SMBus after:

echo -n 1 > /sys/module/i2c_bcm2708/parameters/combined

and now everything works fine, even setting the MAX and MIN values which failed before. PROBLEM SOLVED, thank you very much for your help!

Part 4 :

  • If the above works, thank @joan for her help :)

Notes

  • if you spot any errors or omissions, leave me a comment and I will try to address it as soon as possible
1
  • thanks for the very helpful heads up on the LT part issues, which indicates that this may not be a RPi / Linux issue after all. I will follow your instructions on a fresh install, and hook up an i2c rtc or similar in order to verify the i2c operation.
    – Ol_Devel
    Oct 27, 2016 at 8:16
1

I2C is real easy to use. If you can detect the device, it means it's communicating with the Pi. if you are looking for any specific code please specify else you can find hundreds I2C sample code over here.

1
0

This might not be exactly what you are looking for but the only guide I could find that worked perfectly when I was setting up an RTC was the one at Adafruit.

Try searching for Adding a Real Time Clock to Raspberry Pi - Adafruit pdf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.