We can now bring in readings from the MCP3008 ADC. Let’s take it a step further and provide a visual representation of the measured voltage value using the LED display on the I2C and SPI Education Shield.

Objectives

  1. Accurately measure a voltage using the MCP3008 ADC.
  2. Change the LED pattern to represent a change in the voltage level.

Background
MCP3008 Tutorial 02: Sampling DC Voltage
Shift Register Basics


Schematic
Education Shield – MCP3800 ADC Subsystem
Education Shield – Shift Register Subsystem


Setup
For this module, you’ll need the following equipment:

1. Mate the Education Shield with your Arduino UNO R3. If you’re using the MCP3008 Breakout Board and the Manual Serial Communications Trainer, connect 5V to 5V, 3V3 to 3.3V, GND to GND, AGND to GND, CS to Digital4, MOSI to Digital11, MISO to Digital12, and CLK to Digital13 on the MCP3008 board, and then connect 5V to 5V, GND to GND, CLK to Digital6, LAT to Digital7, DAT to Digital8, CLR to Digital9 and OE to Digital5. That’s a lot of pins!

2. Place jumpers on all the 74HC595 Enable jumpers, and use a jumper to select the 5V VREF.

3. Connect your Arduino to your USB cable, and use the Arduino IDE to upload the “Bare Minimum” sketch from the Basics section of the Examples.


Design Criteria

We are going to use the setup from the previous module, including the connection of the potentiometer. As we turn the dial on the pot, we want the LEDs on the face of the I2C and SPI Education Shield to move from right to left. Higher voltages will move the LEDs left, lower voltages will move the LEDs right.

In order to accomplish this, we’ll need to reuse the code from before, and the refreshShiftRegister() function created in the Shift Register education modules. A certain amount of housekeeping will also have to happen in the setup to make sure all the pins are organized, because we’re using a lot of them.

Happily this isn’t at all terribly difficult!


Code Changes

The code from the previous module is pretty well ready to go, we just have to layer in the shift register commands as necessary.

Declarations

Our declarations section is going to expand to include the pins necessary to manage the shift register.

The “clear” pin exists on the Manual Serial Communications Trainer board, but not on the I2C and SPI Education Shield, so for the Education Shield users, this definition can be omitted as desired.


Setup

As I say above, we need to do some housekeeping in here to bring our board into a ready state for the shift register.

The SPI setup is as before, and we initiate the serial connection (although we’re not using it this time… old habits). Then we establish the mode and state for the OE and CLR pins. CLR must be driven high, otherwise it will continuously blank out any value we attempt to shift out to the LEDs. We’re going to drive OE high though, just until we’ve blanked the shift register to 0x00, then we’ll bring it low to fully enable the outputs. The only reason for doing this is to prevent the little flicker that might occur when we turn the shift register on, then immediately reset all the LEDs off. Persnickity to be sure, but flickers annoy me.

All the shift register pins are outputs, and after setup is complete, we’ll leave the OE and CLR pins in their respective states: low and high.


refreshShiftRegister()

This code is reused from other modules, and is perfectly good for use here.

This takes the byte input “leds” and shifts it out to the 74HC595 in MSBFirst order. That command is wrapped inside the latch pin management that commits the change to the shift register.


Main Loop

Our main loop dispenses with all the voltage calculations from before, and just does a simple check on the value returned from the ADC. There are 1024 possible values, and we have 8 LEDs to display them on. 1024/8 = 128, so each LED will represent 128 chunks of value. From 0 to 128, we’ll turn on one LED, from 128 to 256, we’ll turn on two LEDs, and so on…

And that’s all there really is to it. It’s not terribly elegant, and I’m sure there are users among you who can find far more creative ways of visualizing the output with fading and chasing and peak levels and such. This is just to get you thinking about the ways we can use the ADC output.

Here’s the full code…


Different LED Pattern

Instead of having the bar of LEDs grow to the left, you can also have a single LED move left and right as an indicator.

Replace the if statements with these instead…

Previous Post
MCP3008 Tutorial 02: Sampling DC Voltage
Next Post
MCP3008 Tutorial 04: Sampling Audio Frequency Signals 01
You might also like
Menu