Understanding ADC with the Raspberry Pi Pico and Rust

a comparison chart between analog and digital signals where the analog signal is smooth and the digital signal is choppy

Digital signal sampled from an analog sample

What is ADC?

ADC stands for analog to digital conversion. So since we want to work with numbers or values in our controller programs and not signals or voltages, we’d like to see our analog signals represented as values.

Why is the signal choppy on the Digital Side?

When any sort of metric or value is measured from the physical world, we have to create that measurement at some rate. Analog circuits produce measurements at an incredibly fast rate, basically as fast as an electrical current can complete the circuit where the measurement is taking place.

Digital on the other hand

Requires that we on some sort of clock ‘sample’ the analog value and interpret that into some number. This means that the digital signal will always be slower than the analog version because microcontrollers can’t measure the physical world, we need instrumentation for that, and the place where the circuit touches ‘the real world’ is typically some analog instrument that we sample at some rate.

A quick note on ADC values

They’ll often be quirky numbers that don’t seem to make sense. Like, what is 500 if I’m measuring moisture level? It’s not 500% humidity that’s for sure. I’ve never seen an example of analog instrumentation that produced a signal that was already in SI units. It always requires a bit of translation and/or scaling.

OK, let’s set up to start measuring

The Raspberry Pi Pico has 3 pins where ADC can be read, but we’ll settle for GP26 in the following diagram.

simple diagram of the hw-390 sensor wired up to the Raspberry Pi Pico

This blog has some fantastic circuits drawn up and the diagram of the Pico is much cleaner than the minimalist - crayon sketch I’ve got above.

Once the wiring is setup like above, the Pico will be capable of interpreting signals coming from that sensor.

Prepping the program

I’ve got a getting started post if you’re on your first time getting a Pico up and running with Rust. And I’ve made this super blank pi pico project on GitHub so you don’t have to figure out how to start your own project.

Here is an example file from the rp-hal project on how to get your pins running with the wiring I’ve got laid out above. Their example file starts with GP26 (ADC0) so it should theoretically just work if you compile that example also. I will have some example code coming in future posts on how to do this (I’ll likely update this post) but for now, I haven’t got any example code for this.

But I can say it should only require moving the relevant code from the example file, OR using Cargo to compile the example and loading the example directly onto your project.

A caveat

with the setup covered here, you’ll not know if your program is working or not because there will be no feedback from the controller telling you what value it’s reading from the sensor. Hopefully I’ll get far enough in this project to intercept this post before it publishes so I can create a sample project that will give the ability to get some sort of measurable feedback using the onboard LED to indicate changes in moisture measurement. I’ll also be including a project that shows the value of the measured moisture level on an LCD.

Conclusion

ADC is the process of very quickly sampling analog source signals and creating digital values based on that sample. That value may not make sense and will need to be interpreted by something unless the instrumentation is already producing a digital signal, in which case you won’t need to worry about it.

Previous
Previous

Using the HW-390 with the Raspberry Pi Pico

Next
Next

Soil Moisture Level Automation