golemparts / rppal

A Rust library that provides access to the Raspberry Pi's GPIO, I2C, PWM, SPI and UART peripherals.
MIT License
1.24k stars 97 forks source link

I2C connection with ADS1115 #145

Closed MDH0 closed 5 months ago

MDH0 commented 6 months ago

Hello.

Sorry if the question isn't suited for this place.

Currently I am trying to communicate with an ADS1115 through an I2C connection. But right now I am not 100% sure, if what I did was right.

I was able to get the most useful data with the following code:

use std::error::Error;
use std::thread;
use std::time::Duration;

use rppal::i2c::I2c;

fn main() -> Result<(), Box<dyn Error>> {
    let mut i2c = match I2c::new() {
        Ok(handler) => {handler}
        Err(_) => {panic!("Couldn't create i2c handler!")}
    };
    if let Err(_) = i2c.set_slave_address(0x48) {
        panic!("Couldn't set slave address!")
    }
    let mut message = [0u8; 1];
    message[0] = 0x00;
    if let Ok(written_bytes) = i2c.write(&message) {
        println!("Written {} bytes: {:?}", written_bytes, message);
    } else {
        println!("Failed to write message!");
    }
    for _ in 0..10 {
        let mut buffer = [0u8; 8];
        match i2c.read(&mut buffer) {
            Ok(read_bytes) => println!("Read {} bytes: {:?}", read_bytes, buffer),
            Err(_) => println!("Reading from i2c failed."),
        }
        thread::sleep(Duration::from_secs(5));
    }
    Ok(())
}

On the ADS the ports A0 and A1 are connected to a joystick. This resulted in the following output:

Written 1 bytes: [0] Read 8 bytes: [0, 98, 255, 255, 255, 255, 255, 255] Read 8 bytes: [0, 98, 255, 255, 255, 255, 255, 255] Read 8 bytes: [0, 98, 255, 255, 255, 255, 255, 255] Read 8 bytes: [0, 98, 255, 255, 255, 255, 255, 255] Read 8 bytes: [0, 98, 255, 255, 255, 255, 255, 255] Read 8 bytes: [0, 98, 255, 255, 255, 255, 255, 255] Read 8 bytes: [220, 138, 255, 255, 255, 255, 255, 255] Read 8 bytes: [34, 225, 255, 255, 255, 255, 255, 255] Read 8 bytes: [34, 88, 255, 255, 255, 255, 255, 255] Read 8 bytes: [40, 12, 255, 255, 255, 255, 255, 255]

So right now, at the start of the program it is reading 0 and 98 as the input values and after moving the joystick around, the values change to something somewhat meaningful.

Can someone tell me, if I am moving in the right direction?

I am using an RPi 5.

Thanks for every help.

golemparts commented 6 months ago

As this is unrelated to RPPAL and more of a general ADS111x interface question, this isn't really the right place for this question. However, if anyone can help you out they're of course welcome to.

Unfortunately I don't have any personal experience working with an ADS1115. I suggest making sure you're using the correct I2C functions based on the datasheet (check this example on how RPPAL typically interacts with I2C registers), and perhaps look at how some other ADS111x driver implementations in Rust obtain their readings.

taesungh commented 6 months ago

My team is actually working on a project using the ADS1015 at this moment, and we've been using eldruin/ads1x1x-rs which provides the implementation of the communication protocol for various ADS devices. I would recommend using an existing library rather than trying to reimplement the message formats yourself. The library constructs support embedded-hal traits, so you can use it with RPPAL's I2C instance by including the hal feature.

use ads1x1x::{Ads1x1x, SlaveAddr};
use rppal::i2c::I2c;

fn main() {
    let dev = I2c::new().unwrap();
    let mut adc = Ads1x1x::new_ads1115(dev, SlaveAddr::default());
    adc.read(...)
}