Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Looking for API to read ADC value on Pico #42

Open
abcd-ca opened this issue Sep 26, 2023 · 7 comments
Open

Looking for API to read ADC value on Pico #42

abcd-ca opened this issue Sep 26, 2023 · 7 comments

Comments

@abcd-ca
Copy link

abcd-ca commented Sep 26, 2023

I have a 0-3.25V input wired to GPIO26 (ADC0). Where is the API to read an analog value (0-4095)? I think I'm looking for something like,

let adc_pin_0 = pins.gpio26.into_analog_input();

Update: is it into_floating_input()?

@jannic
Copy link
Member

jannic commented Sep 26, 2023

@abcd-ca
Copy link
Author

abcd-ca commented Sep 26, 2023

Thanks @jannic. So, I've got,

use rp_pico::entry;
use panic_halt as _;
use rp_pico::hal::prelude::*;
use rp_pico::hal::pac;
use rp_pico::hal;

...

    let mut adc = hal::adc::Adc::new(pac.ADC, &mut pac.RESETS);
    let mut adc_pin_0 = hal::adc::AdcPin::new(pins.gpio26.into_floating_input());

...

    loop {
        let pin_adc_counts: u16 = adc.read(&mut adc_pin_0).unwrap();

        delay.delay_ms(2000);
    }
}

But I'm getting this error for read:

method `read` is private [E0624] private method

And this error for its arg

Type mismatch [E0308] expected `u8`, but found `&mut AdcPin<Pin<Gpio26, FunctionSio<SioInput>, PullNone>>` 

@jannic
Copy link
Member

jannic commented Sep 26, 2023

You are missing use embedded_hal::adc::OneShot.
But I see that the error message really isn't nice, as it confuses two different read methods.

@abcd-ca
Copy link
Author

abcd-ca commented Sep 26, 2023

Thank you @jannic. So, I was trying to use what I think is the higher level rp_pico crate for Pico but couldn't find a way to get the adc pin set up and reading. Then I found the first usage example here in the rp2040_hal. I'm waiting for my Pico to arrive so I can't physically test it but the example does compile for me. So the example is using what you referred to. Can I conclude that the rp_pico create is missing an adc API and that I must use the lower level hal crate instead?

@jannic
Copy link
Member

jannic commented Sep 26, 2023

rp_pico isn't really higher level. The board support packages just add board-specific details like pin naming. And they provide the correct boot2 implementation for the external flash chip the board contains.
So there's nothing wrong with directly using the rp2040_hal APIs. They are also re-exported as rp_pico::hal.

jannic added a commit to jannic/rp-hal that referenced this issue Sep 26, 2023
The name conflicts with the public trait method OneShot::read.
This isn't an issue with correct programs, as rustc just uses the
visible function when called from other crates.

But if you forget importing OneShot, you get a confusing error message:

```
error[E0624]: method `read` is private
   --> rp2040-hal/examples/adc.rs:116:45
    |
116 |         let temp_sens_adc_counts: u16 = adc.read(&mut temperature_sensor).unwrap();
    |                                             ^^^^ private method
    |
   ::: /home/jan/rp2040/rp-rs/rp-hal/rp2040-hal/src/adc.rs:302:5
    |
302 |     fn read(&mut self, chan: u8) -> u16 {
    |     ----------------------------------- private method defined here
    |
    = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
    |
15  + use cortex_m::prelude::_embedded_hal_adc_OneShot;
    |
```

Note that the references method in line 302 is not the one that should
be called, and also has different parameters.

(This was noticed in rp-rs/rp-hal-boards#42)
@abcd-ca
Copy link
Author

abcd-ca commented Sep 26, 2023

Thanks, looking forward to getting my Pico to test this out!

jannic added a commit to jannic/rp-hal that referenced this issue Sep 26, 2023
The name conflicts with the public trait method OneShot::read.
This isn't an issue with correct programs, as rustc just uses the
visible function when called from other crates.

But if you forget importing OneShot, you get a confusing error message:

```
error[E0624]: method `read` is private
   --> rp2040-hal/examples/adc.rs:116:45
    |
116 |         let temp_sens_adc_counts: u16 = adc.read(&mut temperature_sensor).unwrap();
    |                                             ^^^^ private method
    |
   ::: /home/jan/rp2040/rp-rs/rp-hal/rp2040-hal/src/adc.rs:302:5
    |
302 |     fn read(&mut self, chan: u8) -> u16 {
    |     ----------------------------------- private method defined here
    |
    = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
    |
15  + use cortex_m::prelude::_embedded_hal_adc_OneShot;
    |
```

Note that the referenced method in line 302 is not the one that should
be called, and also has different parameters.

(This was noticed in rp-rs/rp-hal-boards#42)
@jannic
Copy link
Member

jannic commented Sep 26, 2023

If you like, come to the rp-rs matrix room at https://matrix.to/#/#rp-rs:matrix.org. Nice place to talk, ask questions, share your projects etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants