Skip to content
This repository has been archived by the owner on Jan 11, 2024. It is now read-only.

How to read AIN0-1-2-3 from I2C #65

Closed
oslucchi opened this issue Jan 9, 2014 · 2 comments
Closed

How to read AIN0-1-2-3 from I2C #65

oslucchi opened this issue Jan 9, 2014 · 2 comments
Labels

Comments

@oslucchi
Copy link

oslucchi commented Jan 9, 2014

Hi Natdan/Robert
I'm using PI4J in a university project. It has been great experience so far. Thanks for the hard work you have done

I'm connecting an AD/DA to Raspberry PI based on PCF 8591 (this is the model for your reference http://blog.chrysocome.net/2012/12/i2c-analog-to-digital-converter.html)
I've 4 different moisture sensors i need to get data from. As far as i understand i should be able to connect each of them into the 4 Analog input ports available (AN0-1-2-3).
i was able to read the moisture (1 byte of data) via gpio -x pcf8591:0x48:120 aread 120.
I was only able to read data from sensor connected to pin AN0. Nothing came from sensors connected to pins A1-2-3.
Questions for you are:

  • do you have a working example on how to perform the same read via PI4J?
  • is there any example or document explaining how i can read data from all other sensors?

Looking forward to your response and again thanks very much for your work!

@oslucchi
Copy link
Author

i got the answer :)
the java snippet for those who are interested is:
private I2CBus bus = I2CFactory.getInstance(0x1);
private I2CDevice device = bus.getDevice(0x48);

    try 
    {
        devoce.write((byte) (0x40 | (sensorId & 3)));
        devoce.read();
        buffer = (byte) devoce.read();
        unsignedValue = (short) ((short) 0x00FF & buffer);
    }
    catch (IOException e) 
    {
        logger.error("IOEXception " + e.getMessage() + " reading sensor " + sensorId);
        return false;
    }

the first read is meant to discard the unsignificant response byte

@narendranss
Copy link

What is sensorId value in the above snippet of program?

mheath added a commit to mheath/pi4j that referenced this issue Feb 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants