Implementing ADC using Raspberry Pi and MCP3008

Several years ago I wrote about adding analog-to-digital capabilities to the Raspberry Pi. At that time, I used an ATtinyx61 series MCU to provide ADC capabilities, communicating with the RPi via an I2C interface. In retrospect it was much more complicated than necessary. What follows is an attempt to re-do that project using an MCP3008, a 10 bit ADC that communicates on the SPI bus.

MCP3008 device

The MCP3008 is an 8-channel 10-bit ADC with an SPI interface^[Datasheet can be found here.]. It has a 4 channel cousin, the MCP3004 that has similar operating characteristics. The device is capable of performing single-ended or differential measurements. For the purposes of this write-up, we’ll only concern ourselves with single-ended measurement. A few pertinent details about the MCP3008:

  • It is capable of conversion rates of around 200 kilosamples per second.
  • It operates on SPI modes 0,0 or 1,1^[The SPI bus can operate in different ways depending on the clock polarity and phase and how the data relates to clock transitions. “Mode 0,0” means that the clock polarity is 0 and its phase is 0 whereas “mode 1,1” means that the clock polarity and phase are both 1.]

If you have done any work with SPI, you’ll know that there are 4 signals. MOSI stands for “master out, slave in” whereas MISO stands for “master in, slave out”. The two other signals are the clock which provides a time standard for the digital transaction and the SS (slave select), also called CE (chip enable) or CS (chip select.)

SPI communication in 8-bit read/write frames

In this example, we are going to use an SPI library to communicate with the MCP3008 in 8-bit frames, so the pertinent section of the datasheet is on page 21, section 6.1 Using the MCP3004/MCP3008 with Microcontroller (MCU) SPI Ports. The Figure 6-1 (reproduced below) shows how we will go about communicating with the device over the SPI bus.

From the communication diagram above, we get an excellent overview of the entire transaction. First, we must drop CS to initiate the transaction. With the CS low, we begin clocking in and out data. Figure 6-1 shows that we must clock in a single start bit (0x01) followed by mode and channel select bits. Table 5-2 shows the configuration bits that we must clock-in to return an ADC reading.

For example, if we wish to make a single-ended reading on channel 0, we must clock in the bits 1000. Note from figure 6-1, we must shift the bits by 4 binary places, so that for a single-ended reading from channel 0, we would clock in 0b1000000 or 0x80.

Software implementation

I chose to implement this in C rather than Python this time. There are a handful of libraries for the BCM2835. I used the bcm2835 library which is excellent. It is low-level enough that I can what’s going on, but not completely “bare metal” programming. You can find out more about the spi module of this library.

I will start with the code section-by-section then provide a link to the entire source code. First, of course, you’ll need to install the library. You can find a version-agnostic install script here. I used it; it works.

First, we’ll include a couple libraries that we need, and set up three constants. The first is the 0b00000001 that we need to transfer as the start bit. The second is the end bits 0b00000000 that we clock in to the MCP3008 so that we can clock out 8 bits of the ADC value. Finally, since I set up my test circuit to measure on channel 0, I just define a constant for that.

#include <stdio.h>
#include <bcm2835.h>

uint8_t start = 0x01;
uint8_t end = 0x00;
uint8_t chan = 0x00;

Next I declare my function prototypes. Just C business as usual.

int readADC(uint8_t chan);
float volts_adc(int adc);

In the body of main, I start by testing whether I can initiate the SPI interface on the Pi:

if (!bcm2835_init())
{
  printf("bcm2835_init failed. Are you running as root??\n");
  return 1;
}

if (!bcm2835_spi_begin())
{
  printf("bcm2835_spi_begin failed. Are you running as root??\n");
  return 1;
}

If we pass those tests, we’re ready to go. Let’s set up the interface.

bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default

To read the ADC value, we have to prepare the bytes that we’ll clock in first. All of that is done in a function readADC.

int readADC(uint8_t chan){
  char buf[] = {start, (0x08|chan)<<4,end};
  char readBuf[3];
  bcm2835_spi_transfernb(buf,readBuf,3);
  return ((int)readBuf[1] & 0x03) << 8 | (int) readBuf[2];
}

It looks like there’s a lot going on here, but basically we are performing bit manipulations to get the input bits in the right order and the same for the output bits. First we declare an output buffer buf[] whose contents are three bytes. The first is the start bit 0b00000001, followed by the mode selections bytes, and terminated by a junk byte so that we can finish clocking out the resulting data. How do we interpret the value of (0x08|chan)<<4? Start from the inside of the parenthesis. 0x08 is 0b00001000 where the 1 bit here represents the selection of single-ended mode on the ADC. We logical OR that with the channel that we want to read. Finally, outside the parenthesis, we shift it over by 4 bits so these bits are in the upper nibble. Remember we have to clock in the data MSB first?

Next we declare an input buffer readBuf[3] to hold the data we’re reading in. Then we perform a 3 byte transfer. Now, what do we do with the results? Ouch. Well, remember we reading in 3 bytes. The first lines up with our start bit, so it’s junk and we’ll just ignore readBuf[0]. What about the next byte readBuf[1]? From Figure 6 of the datasheet, you can see that we only care about the 2 lower bits of the first byte which will become the upper two bits of the 10-bit ADC result. First we logical AND those with 0x03 (0b00000011) to get rid of anything above the first two bits. Then we shift it over by 8 bits, so that when we logical OR it with the lower 8 bits in readBuf[2] it coheres into a single 16 bit int. The casts just keep everything in 16 bits along the way.

Real life

So, does the software work? We can test it by applying a logical probe instrument and find out. I used an Intronix logic analyzer to watch the conversion. Here’s the result:

SPI bus analysis

Compare the logic analyzer image to the datasheet. Looks similar! On the MISO line, we can ignore the first byte 0x07. With the second byte, 0xFB (0b11111011) we only care about the bottom two bits (11). In the third byte, we use all 8 bits. Putting those 10 bits together we have 0b1111111111 or 0x3FF, 1023 decimal. That’s the largest number we can express in 10 bits. That’s because I tied channel 0 to the 3.3v out of the Raspberry Pi. Now we can calculate the voltage. Using the reference of 3.3v, the ADC value of 1023 represents 3.3v and we can compute an arbitrary value using a function:

float volts_adc(int adc) {
  return (float)adc*3.3f/1023.0f;
}

And that’s it - a working example of reading the MCP3008 using C on the Raspberry Pi. If you’d like the entire code for the example application, you can find the gist here..

References

2018: Experiment No. 1

2018 is my year of experiments (Why? TL;DR: New Year’s resolutions are over-rated and have a high failure rate. Anyone can run an experiment for a month.) My first experiment (No news for a month) is nearly done and I’ll declare it a success.

Background

The round-the-clock sensational news cycle exists in large part to create wealth for the already-too-wealthy. Little of it is actionable, leaving us at the same time both outraged and impotent. Mostly I decided to give up on the news because of Donald Trump, the demented psychopathic moron who managed to get elected president.^[I use these terms very carefully. Many have speculated that he suffers from some form of dementia owing to events where he slurs his words and perseverates. His sociopathic or psychopathic behaviours are well-documented; he is man devoid of empathy. And finally, his lack of reading is well-known. For all I can tell, the man is a functional illiterate. In contrast, his predecessor is a bibliophile and read widely and voraciously throughout his tenure.] Since Trump took office, like others, I’ve found myself cycling repeatedly through the stages of grief. But mostly I’ve been stuck on anger. There’s something about willful ignorance that does that to me.

Experiment

The methodology was simple. I simply willed myself to avoid the news for an entire month. After briefly considering the use of tools that would block news websites, I decided to go cold-turkey.

Results

Some of the things that I noticed:

  • Airports are saturated with news. I travelled a bit during the month. With TV’s blaring the news in every terminal area, it’s impossible to avoid hearing the news. I learned that a book highly critical of Trump was published and that the man himself was displeased. I learned that Congressional Republicans are trying to stop Special Counsel Robert Mueller’s investigation without looking like that’s what they’re doing.
  • Social media can be a significant vector of news. The sidebar on Facebook likes to trumpet the latest bush crash, earthquake, and political twist. But I also discovered that you can resize your browser to make the sidebar go away. Presto!
  • I tended to want to look at the news when I was bored. If I had a moment of boredom, I’d think about the news. Given that the news is supposed to serve in large part the factual needs of an informed electorate, seeking it out of boredom is more in keeping with the values of the entertainment industry, not those of journalism.
  • Outsourcing the news to others slows down the cycle. It was impossible to avoid the news completely. I heard others talking about political happenings and other current events. In fact, I even asked about them. But by outsourcing the news-seeking to others, I was able to slow down the process and keep it at a distance in a way that made it seem more abstract. I didn’t feel as outraged.
  • I felt more productive Once I eliminated the desire to read the news, I was able to stay with purposeful tasks longer.

Conclusions

After a month of no news, I miss reading good journalism. I may go back to it. Or I may not. The experiment was such a success that it would be hard to go back. The real problem for most of us is that the overlap between our circle of interest (what’s going on in the world) and our circle of influence is very small. David Cain noticed the same thing when he quit the news: “Being concerned makes us feel like we’re doing something when we’re not."

Now off to my next experiment - a month of practicing a secular technology “sabbath”.

2018: A year of experiments

New Year’s resolution time is at hand. But not for me; at least not in a traditional sense. I was inspired by David Cain’s experiments. In short, he conducts monthly experiments in self-improvement. The idea of an experiment is appealing in ways that a resolution is not. A resolution presumes an outcome and relies only on the long application of will to see it through. An experiment on the other hand, makes only a conjecture about the outcome and can be conducted for a shorter period.

Peering into Anki using R

Yet another diversion to keep me from focusing on actually using Anki to learn Russian. I stumbled on the R programming language, a language that focuses on statistical analysis. Here’s a couple snippets that begin to scratch the surface of what’s possible. Important caveat: I’m an R novice at best. There are probably much better ways of doing some of this… Counting notes with a particular model type Here we’ll use R to do what we did previously with Python.

Language word frequencies

Since one of the cornerstones of my approach to learning the Russian language has been to track how many words I’ve learned and their frequencies, I was intrigued by reading the following statistics today: The 15 most frequent words in the language account for 25% of all the words in typical texts. The first 100 words account for 60% of the words appearing in texts. 97% of the words one encounters in a ordinary text will be among the first 4000 most frequent words.

Anki database adventures: Counting notes by model type

Continuing my series on accessing the Anki database outside of the Anki application environment, here’s a piece on accessing the note type model. You may wish to start here with the first article on accessing the Anki database. This is geared toward mac OS. (If you’re not on mac OS, then start here instead.) The note type model Since notes contain flexible fields in Anki, the model for a note type is in JSON.

Accessing the Anki database with Python: Working with a specific deck

I previously wrote about accessing the Anki database using Python on mac OS. Extending that post, I’ll show how to work with a specific deck in this short post. To use a named deck you’ll need its deck ID. Fortunately there’s a built-in method for finding a deck ID by name: col = Collection(COLLECTION_PATH) dID = col.decks.id(DECK_NAME) Now in queries against the cards and notes tables we can apply the deck ID to restrict them to a certain deck.

Working with the Anki database on mac OS using Python

Not long ago I ran across this post detailing a method for opening and inspecting the Anki database using Python outside the Anki application environment. However, the approach requires linking to the Anki code base which is inaccessible on mac OS since the Python code is packaged into a Mac app on this platform. The solution I’ve found is inelegant; but just involves downloading the Anki code base to a location on your file system where you can link to it in your code.