Tutorials on code for Arduino with external ADC? - Page 1
Forum section: electronics / general

author: David Aurora
author_rank: Frequent Contributor
timestamp: December 05, 2022, 02:16:20 am
content: 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.

author: ledtester
author_rank: Super Contributor
timestamp: December 05, 2022, 02:50:33 am
content: 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 Electronicshttps://youtu.be/ZGaCXHvgcE4Here is a video which explains the code for interfacing with the MC3208 using the SPI device:Arduino | Tutorial | MCP3208 Interfacing -- WGLabzhttps://youtu.be/qLcmI5xcN58and 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 Carterhttps://youtu.be/gfKdITStXioYou might find the bit-banging code easier to follow as it shows exactly which GPIO pins are being set high/low.

author: David Aurora
author_rank: Frequent Contributor
timestamp: December 05, 2022, 03:32:45 am
content: Quote from: ledtester on December 05, 2022, 02:50:33 amThanksI 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.

author: bidrohini
author_rank: Regular Contributor
timestamp: December 05, 2022, 07:24:45 am
content: You can see this too:https://electronoobs.com/eng_arduino_tut83.php

author: David Aurora
author_rank: Frequent Contributor
timestamp: December 05, 2022, 08:44:21 am
content: Quote from: bidrohini on December 05, 2022, 07:24:45 amThanks. 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 codeNot 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

author: ledtester
author_rank: Super Contributor
timestamp: December 05, 2022, 12:54:54 pm
content: Quote from: bidrohini on December 05, 2022, 07:24:45 amNote that the ADS1115 uses I2C.Quote from: David Aurora on December 05, 2022, 08:44:21 amEach 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/allNow 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 athttps://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 thecommandbitsvariable. You can look up what those bits mean on page 19 of the MCP3208 datasheet:https://playground.arduino.cc/Code/MCP3208/

author: Lindley
author_rank: Regular Contributor
timestamp: December 05, 2022, 03:51:49 pm
content: You could use the ESP32 / Arduino IDE with its inbuilt 12bit ?eghttps://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/