Author Topic: Tutorials on code for Arduino with external ADC?  (Read 2167 times)

0 Members and 1 Guest are viewing this topic.

Offline David AuroraTopic starter

  • Frequent Contributor
  • **
  • Posts: 422
  • Country: au
Tutorials on code for Arduino with external ADC?
« on: December 05, 2022, 02:16:20 am »
I'm suuuuuper rusty with Arduino (99.999% of my work is analog electronics) but I've been designing a bit of test gear where I measure a bunch of analog voltages, do some maths to it and spit out the results on an LCD.

Using an Arduino Nano Every with its built in ADCs everything is working fine, but I wanted to switch to an external 12 bit ADC for more resolution. I opened up an example I found on the Arduino site for an MCP3208 (https://playground.arduino.cc/Code/MCP3208/) thinking I could get a feel for it from there and then add the relevant code to my design, but the example code doesn't actually explain anything about how it works or what any of it means, and all the tutorials I could find completely skip over the code and just focus on how to wire things up on a breadboard.

Does anybody know of any tutorials that go through the basics of writing code for external ADCs with Arduino? Like not just "Copy and paste this code and you'll see numbers in the serial output" but actual explanations like "The line ____ tells the microcontroller to read from input __ and store it as ____", etc.
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3035
  • Country: us
Re: Tutorials on code for Arduino with external ADC?
« Reply #1 on: December 05, 2022, 02:50:33 am »
External ADCs use either SPI or I2C to communicate with them.

The MCP3208 is an SPI device. SPI on an Arduino Nano can be done either by "bit-banging" (i.e. the program explicitly drives the GPIO pins) or by using the SPI device in the microcontroller.

Here is a video which explains the spi-demo.c example from the Arduino library:

Arduino Workshop - Chapter 5 - Using SPI -- Core Electronics
https://youtu.be/ZGaCXHvgcE4

Here is a video which explains the code for interfacing with the MC3208 using the SPI device:

Arduino | Tutorial | MCP3208 Interfacing -- WGLabz
https://youtu.be/qLcmI5xcN58

and here is a video which uses bit-banging code to interact with the chip:

Basics of connecting the MCP3208 12 bit ADC to Arduino -- Simon Carter
https://youtu.be/gfKdITStXio

You might find the bit-banging code easier to follow as it shows exactly which GPIO pins are being set high/low.

 

Offline David AuroraTopic starter

  • Frequent Contributor
  • **
  • Posts: 422
  • Country: au
Re: Tutorials on code for Arduino with external ADC?
« Reply #2 on: December 05, 2022, 03:32:45 am »
External ADCs use either SPI or I2C to communicate with them.

The MCP3208 is an SPI device. SPI on an Arduino Nano can be done either by "bit-banging" (i.e. the program explicitly drives the GPIO pins) or by using the SPI device in the microcontroller.

Here is a video which explains the spi-demo.c example from the Arduino library:

Arduino Workshop - Chapter 5 - Using SPI -- Core Electronics
https://youtu.be/ZGaCXHvgcE4

Here is a video which explains the code for interfacing with the MC3208 using the SPI device:

Arduino | Tutorial | MCP3208 Interfacing -- WGLabz
https://youtu.be/qLcmI5xcN58

and here is a video which uses bit-banging code to interact with the chip:

Basics of connecting the MCP3208 12 bit ADC to Arduino -- Simon Carter
https://youtu.be/gfKdITStXio

You might find the bit-banging code easier to follow as it shows exactly which GPIO pins are being set high/low.

Thanks

I saw the last two videos earlier. The second one was excruciating to follow with the sound quality but I'll give it another chance, the last one doesn't explain anything about the code at all though.

Hadn't checked out the first one, will do though.
 

Offline bidrohini

  • Regular Contributor
  • *
  • Posts: 201
  • Country: bd
Re: Tutorials on code for Arduino with external ADC?
« Reply #3 on: December 05, 2022, 07:24:45 am »
You can see this too:https://electronoobs.com/eng_arduino_tut83.php
 

Offline David AuroraTopic starter

  • Frequent Contributor
  • **
  • Posts: 422
  • Country: au
Re: Tutorials on code for Arduino with external ADC?
« Reply #4 on: December 05, 2022, 08:44:21 am »
You can see this too:https://electronoobs.com/eng_arduino_tut83.php

Thanks. In that one it looks like basically everything is handled in libraries, which is handy if you have that particular chip on hand I guess, but still doesn't really teach me anything beyond copy/pasting code

Not that I'm hell bent on doing this the hard way, I may just order whatever has the best library available and make life easy. I'm just kind of surprised that I haven't found a proper walkthrough of writing code for reading ADCs
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3035
  • Country: us
Re: Tutorials on code for Arduino with external ADC?
« Reply #5 on: December 05, 2022, 12:54:54 pm »
You can see this too:https://electronoobs.com/eng_arduino_tut83.php

Note that the ADS1115 uses I2C.


Not that I'm hell bent on doing this the hard way, I may just order whatever has the best library available and make life easy. I'm just kind of surprised that I haven't found a proper walkthrough of writing code for reading ADCs

Each ADC has it's own command protocol built on top of the SPI protocol so you're not going to see a general way of interacting with ADCs.

The way to interact with a general SPI device is this:

1. First select the device by pulling LOW the CS line of the device.
2. Send a sequence of bytes one bit at a time through a GPIO line using another GPIO line as a clock signal; one bit is sent per clock transition.
3. Continue toggling the clock and receive the result through another GPIO line.
4. Finally raise the CS line of the device to indicate the end of the interaction.

This page has a pretty good description of this process:

https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi/all

Now that you can send and receive results you need to look at your specific device's datasheet to determine what to send and how to interpret the results.

The code at https://playground.arduino.cc/Code/MCP3208/ has the convenient function read_adc() which will do all of this for you. The command it sends are the top 5 bits of the commandbits variable. You can look up what those bits mean on page 19 of the MCP3208 datasheet:

https://playground.arduino.cc/Code/MCP3208/
 
The following users thanked this post: David Aurora, Ian.M

Offline Lindley

  • Regular Contributor
  • *
  • Posts: 195
  • Country: gb
Re: Tutorials on code for Arduino with external ADC?
« Reply #6 on: December 05, 2022, 03:51:49 pm »
You could use the ESP32 / Arduino IDE with its inbuilt 12bit ?

eg  https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf