Message Boards Message Boards

Setup an i2c interface?

Posted 7 years ago

I want to use an analogue sensor with Mathematica on the Raspberry Pi, so I connected a I2C ADC interface. The ADC works fine (it registers on 0x50 of the I2C bus and I can read values from it with Python) I followed the I2C Setup as described in http://reference.wolfram.com/language/ref/device/I2C.html , but I do not get I2C devices with FindDevices[] Anyone a clue? Is the documentation already updated to a new version of Mathematica that is not released yet?

Thanks Ted

POSTED BY: Ted vanderTogt
6 Replies
Posted 7 years ago

Thanks Ian, that does make sense :-)

For the ADC / rotation sensor: I started with a solution like rotation[{x,y}]:= (x * 256 +y )/(12 * 256+240) * 300 degrees, and then rotation[DeviceRead["I2C",2]] It worked too, but you just learned me some new functions :-)

Great that the SenseHAT works now too. I did not see it with FindDevices[] first, (I did see a lot of I2C entries) but all examples work beautifully . Great job!

For the students I will start with a I2C soil moisture sensor that needs some calibration and conversion (it is non linear) . I want to make it as smooth as the SenseHAT functions, and then I will add other I2C sensors like UV index, Sunlight, Infrared, temperature, humidity. Can I peek into some code of how the SenseHAT functions are built up? or is that closed source?

Thanks for your help,

Ted

POSTED BY: Ted vanderTogt

You can see the source code for the SenseHAT in this file:

First@FileNames["*", FileNameJoin[{Lookup[PacletInformation["RaspberryPiTools"], "Location"], "Kernel"}]]

Ian

POSTED BY: Ian Johnson
Posted 7 years ago

Hi Ian,

I am making a simple example on how to hook up an analogue sensor to the Raspberry Pi and read it with Mathematica. Since the Pi has no analogue inputs I use an ADC. In my example I start with a Grove Rotary Angle Sensor (potentiometer) and a Grove I2C ADC (12bits).

sudo i2cdetect -y 1 shows me 1 device with address 50

FindDevices["I2C"] gives me 1 instance: class I2C; ID1; status not connected (80),,, ( I wonder why the 80 and not 50...)

If I then DeviceRead[dev, 2], I get values from {0,1} to {12,240} Since it is a 12 bit ADC I suppose I need to read two bytes and convert these together into one value of 0 to 300 degrees. I think I understand how to do that, but it would be nice if I would see how others work out such functions in the most generic way. So I could make a library of sensor functions (in a package?) and my students can focus on their experiments. I wonder if the SenseHAT is already supported now: If so, I would love to look on how that is done.

Thanks,

Ted

POSTED BY: Ted vanderTogt

As per the SenseHAT, yes it is supported, you can see documentation for it in the documentation center of a 11.0 desktop or cloud Mathematica session or else online at : http://reference.wolfram.com/language/ref/device/SenseHAT

As per determining the actual temperature from the bytes you read back, that process will likely be quite different for different kinds of sensors, but for a simple temperature sensor that's 12 bits and returns this kind of format, I imagine that it would probably be something as simple shifting the first number up by 8 bits and or'ing the two together, i.e.

BitOr[BitShiftLeft[#1,8],#2]&@@DeviceRead["I2C",2]

Does that make sense?

Ian

POSTED BY: Ian Johnson
Posted 7 years ago

Just found out that with the new Mathematica version 11 for the Raspberry Pi, the i2c interface works. Now I am trying to find out what to read and what to write to different sensors. (the standard Arduino and Python examples that come with the sensors being not applicable)... If anyone has examples, that would be very welcome.

POSTED BY: Ted vanderTogt

Hi Ted,

What kind of example are you looking for?

With the I2C device driver you can read and write to an I2C device by first locating the device with :

dev = DeviceOpen["I2C",FromDigits["50",16]]

And then write bytes to it with

DeviceWrite[dev,bytes]

And read from it with:

DeviceRead[dev,numberOfBytes]

Thanks,

Ian

POSTED BY: Ian Johnson
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract