Exploring Edison - SPI AtoD with the SPI Bus
Exploring Edison - SPI AtoD with the SPI Bus
Written by Harry Fairhead   
Article Index
Exploring Edison - SPI AtoD with the SPI Bus
Configuration and Protocol
How Fast?

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.

This is a chapter from our ebook on the Intel Edison. The full contents can be seen below. Notice this is a first draft and a work in progress. Use the comments or email harry.fairhead@i-programmer.info with your queries or suggestions.



Now On Sale!

You can now buy a print edition of Exploring Intel Edison.
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 

Chapter List

  1. Meet Edison
    In this chapter we consider the Edison's pros and cons and get an overview of its structure and the ways in which you can make use of it. If you have ever wondered if you need an Edison or an Arduino or even a Raspberry Pi then this is the place to start. 

  2. First Contact
    When you are prototyping with the Edison you are going to need to use one of the two main breakout boards - the Arduino or the mini. This chapter explains how to set up the Edison for both configurations. 

  3. In C
    You can program the Edison in Python, JavaScript or C/C+ but there are big advantages in choosing C. It is fast, almost as easy as the other languages and gives you direct access to everything. It is worth the effort and in this chapter we show you how to set up the IDE and get coding. 

  4. Mraa GPIO
    Using the mraa library is the direct way to work with the GPIO lines and you have to master it. Output is easy but you do need to be aware of how long everything takes. Input is also easy but using it can be more difficult. You can use polling or the Edison interrupt system which might not work exactly as you would expect.

  5. Fast Memory Mapped I/O
    There is a faster way to work with GPIO lines - memory mapped I/O. Using this it is possible to generate pulses as short at 0.25 microsecond and read pulse widths of 5 microseconds. However getting things right can be tricky. We look at how to generate fast accurate pulses of a given width and how to measure pulse widths.

  6. Near Realtime Linux 
    You need to be aware how running your programs under a non-realtime operating system like Yocto Linux effects timings and how accurately you can create pulse trains and react to the outside world. In this chapter we look the realtime facilities in every version of Linux. 

  7. Sophisticated GPIO - Pulse Width Modulation 
    Using the PWM mode of the GPIO lines is often the best way of solving control problems. PWM means you can dim an LED or position a servo and all using mraa. 

  8. Sophisticated GPIO -  I2C 
    I2C is a simple communications bus that allows you to connect any of a very large range of sensors. 

  9. I2C - Measuring Temperature  
    After looking at the theory of using I2C here is a complete case study using the SparkFun HTU21D hardware and software. 
  10. Life At 1.8V
    How to convert a 1.8V input or output to work with 5V or 3.3V including how to deal with bidirectional pull-up buses.

  11. Using the DHT11/22 Temperature Humidity Sensor at 1.8V 
    In this chapter we make use of all of the ideas introduced in earlier chapters to create a raw interface with the low cost DHT11/22 temperature and humidity sensor. It is an exercise in interfacing two logic families and implementing a protocol directly in C. 

  12. The DS18B20 1-Wire Temperature 
    The Edison doesn't have built in support for the Maxim 1-Wire bus and this means you can't use the very popular DS18B20 temperature sensor. However with a little careful planning you can and you can do it from user rather than kernel space. 

  13. Using 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. 

  14. SPI in Practice The MCP3008 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.

  15. Beyond mraa - Controlling the features mraa doesn't. 
    There is a Linux-based approach to working with GPIO lines and serial buses that is worth knowing about because it provides an alternative to using the mraa library. Sometimes you need this because you are working in a language for which mraa isn't available. It also lets you access features that mraa doesn't make available. 




The Edison with the mini-breakout board doesn't have any analog inputs or outputs. You could move up to the Arduino board but this is physically large and overkill.  A simpler solution is to interface an MCP3008 directly to the Edison's SPI bus. It is also a good example of working with the SPI bus. 

The MCP3000 family of AtoD converters provides a simple, cheap and low cost alternative to fitting an entire expansion board. Although the MCP3008 with 8 AtoD inputs and the MCP3004 with 4 AtoD inputs at 10-bit 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:

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, the most significant bit is a zero; and if it is more than or equal to it 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. 


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 converter 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 between 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.

Last Updated ( Monday, 20 June 2016 )

RSS feed of all content
I Programmer - full contents
Copyright © 2017 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.