The SPI bus can be difficult to make work at first but once you know what to look for about how the slave claims to work it gets easier. To demonstrate how its done let's add eight channels of 12 bit AtoD using the MCP3008.


Now On Sale!

You can now buy a print edition of micro:bit IoT in C.

You can buy it from:

USA and World  Amazon.com
Canada              Amazon.ca
UK                      Amazon.co.uk
France                Amazon.fr
Germany            Amazon.de
Spain                  Amazon.es
Brazil                  Amazon.br
Italy                    Amazon.it
Japan                 Amazon.co.jp
Mexico               Amazon.com.mx 

 

 The full contents can be seen below. 

Chapter List

  1. Getting Started With C/C++
    Anyone who wants to use the BBC micro:bit to its full potential as an IoT device needs to look outside the coding environments provided by its own website. As an mbed device, however, the micro:bit  is capable of being programmed in C/C++. Here we look at how to use the mbed online compiler for a simple demo program.

  2. Offline C/C++ Development  
    We have already discovered how to use the online editor to create a C/C++ program. Now we are going to move to the desktop with an offline approach. This has the advantage that we can use any tools we care to select and no Internet connection is needed.
  3. First Steps With The GPIO 
    The most basic task when working with the micro:bit is controlling the I/O lines. This isn't difficult if you use the framework provided but there some subtle points to watch out for. This chapter looks a the basics of using the GPIO.

  4. Working Directly With The Hardware - Memory Mapping. 
    The framework makes working with the GPIO and other devices as easy as it can be but there are many layers of software to go through before you get to the hardware. Writing directly to the hardware can make things up to ten times faster and give you access to things that their framework doesn't. It is also an educational experience to deal with the raw hardware directly.

  5. Pulse Width Modulation, Servos And More
    In this chapter we take a close look at pulse width modulation PWM including, sound, driving LEDs and servos.

  6. I2C
    The I2C bus is one of the most useful ways of connecting moderately sophisticated sensors and peripherals to the any processor. The only problem is that it can seem like a nightmare confusion of hardware, low level interaction and high level software. There are few general introductions to the subject because at first sight every I2C device is different, but here we present one.

  7. I2C Temperature Measurement
    Using I2C devices is fairly easy once you have successfully used one - and hence know what information you need and what to look for in a working system. In this chapter we use the HTU21D temperature and humidity sensor as a case study of I2C in action. It also happens to be a useful sensor.

  8. A Custom Protocol - The DHT11/22

  9. The DS18B20 - One Wire Bus

  10. The SPI Bus
    The SPI bus can be something of a problem because it doesn't have a well defined standard that every device conforms to. Even so if you only want to work with one specific device it is usually easy to find a configuration that works - as long as you understand what the possibilities are. 

  11. SPI MCP3008/4 AtoD   
    The SPI bus can be difficult to make work at first but once you know what to look for about how the slave claims to work it gets easier. To demonstrate how its done let's add eight channels of 12 bit AtoD using the MCP3008.

  12. Serial Connections
    The serial port is one of the oldest of ways of connecting devices together but it is still very, very useful. The micro:bit has a single serial interface but it can be directed to use any of the GPIO piins as Rx and Tx. 

  13. WiFi 
    The micro:bit has a radio that works in Bluetooth LE and point-to-point ad-hoc mode, but at the moment it lacks WiFi connectivity. The solution is to use the low cost ESP8266 to make the connection via the micro:bit's serial port. 

  14. LED Display 
    The micro:bit's LED display may only be 5x5 but it is very versatile. If you want to make use of it directly then you are going to have to master some lower level functions.

 

 

The micro:bit has a single analog to digital converter which can be connected to any of the GPIO lines designated as analog pins. Sometimes however this isn't enough.  The MCP3000 family of AtoD convertors provides a simple, cheap and low cost solution. Although the MCP3008 with 8 AtoD inputs and the MCP3004 with 4 AtoD inputs at 10 bits precision are the best known there are other devices in the family including 12 and 13 bit precision and differential inputs at around the same sort of cost $1 to $2.

In this chapter the MCP3008 is used because it is readily available and provides a good performance at low cost but the other devices in the family work in the same way and could be easily substituted.

 

The MCP3008

The MCP3008 is available in a number of different packages but the standard 16 pin PDIP is the easiest to work with using a prototyping board. You can buy it from the usual sources including Amazon if you need one in a hurry. 

Its pin outs are fairly self explanatory:
pinout

You can see that the analog inputs are on the left and the power and SPI bus connections are on the right.  The conversion accuracy is claimed to be 10 bits but how many of these bits correspond to reality and how many are noise depends on how you design the layout of the circuit.

You need to take great care if you need high accuracy. For example you will notice that there are two voltage inputs VDD and VREF. VDD is the supply voltage that runs the chip and VREF is the reference voltage that is used to compare the input voltage. Obviously if you want highest accuracy VREF, which has to be lower than or equal to VDD, should be set by an accurate low noise voltage source - however in most applications VREF and VDD are simply connected together and the usual, low quality supply voltage is used as the reference. If this isn't good enough then you can use anything from a zener diode to a precision voltage reference chip such as the TL431. At the very least however you should add a 1uF capacitor between the VDD pin and the VREF pin to ground. 

The MC3000 family is a type of AtoD called a successive approximation converter. You don't need to know how it works to use it but it isn't difficult. The idea is that first a voltage is generated equal to VREF/2 and the input voltage is compared to this. If it is less then the most significant bit is a zero and if it is more or equal then it is a one. At the next step the voltage generated is VREF/2+VREF/4 and the comparison is repeated to generate the next bit. 

 

block

 

You can see that successive approximation fits in well with a serial bus as each bit can be obtained in the time needed to transmit the previous bit. However the conversion is relatively slow and a sample and hold circuit has to be used to keep the input to the convertor stage fixed. The sample and hold takes the form of a 20pF capacitor and a switch. The only reason you need to know about this is that the conversion has to be complete in a time that is short compared to the discharge time of the capacitor - so for accuracy there is a minimum SPI clock rate as well as a maximum. 

Also to charge the capacitor quickly enough for it to follow a changing voltage it needs to be connected to a low impedance source. In most cases this isn't a problem but if it is you need to include an op amp. If you are using an op amp buffer then you might as well implement a filter to remove frequencies from the signal that are too fast for the AtoD to respond to - an anti-aliasing filter. How all this works takes us into the realm of analog electronics and signal processing and well out of the core subject matter of this book. 

You can also use the AtoD channels in pairs - differential mode - to measure the voltage difference them. For example, in differential mode you measure the difference between CH0 and CH1 i.e. what you measure is CH1-CH0. In most cases you want to use all eight channels in single-ended mode. 

In principle you can take 200K samples per second but only at the upper limit of the supply voltage VDD=5V falling to 75K samples per second at its lower limit of  VDD=2.7V. 

The SPI clock limits are a maximum of 3.6MHz at 5V and 1.35MHz at 2.7V. The clock can go slower but because of the problem with the sample and hold mentioned earlier it shouldn't go below 10kHz.

How fast we can take samples is discussed later in this chapter.

Connecting MCP3008 To micro:bit

The connection to the PI's SPI bus is very simple and can be seen in the diagram below.

The only additional component that is recommended is a 1uF capacitor connected between pins 15 and 16 to ground mounted as close to the chip as possible. As discussed in the previous section you might want a separate voltage reference for pin 15 rather than just using the 3.3V supply.