Raspberry Pi Analog to Digital Conversion Experiments and Howto
Software Support
I wrote Python functions to support the multiplexed SPI ADC devices using RPi.GPIO and SPIDEV, however any software that has support for the MCP3208 can easily be modified to support the multiplexer.
(Top view of the data acquisition board – click on the image for a larger version)
Simply select the SPI device 0-7 by setting GPIO’s 23,24,25 to the binary value of 0-7 (GPIO23 is the LSB, GPIO25 is the MSB) before communicating with the selected SPI device.
The code below selects SPI device 1 on /CE1, and reads all eight channels every 15 seconds. This ADC is configured for 0-8.192V in 2mV steps with voltage dividers.
I made the following connections:
Channel 0: Pi’s 3.3V supply from the Pi GPIO header
Channel 1: not used
Channel 2: 4.096 voltage reference
Channel 3: Pi’s 5V supply from the GPIO header
Channel 4: not used
Channel 5: Sharp 4cm-30cm distance sensor
Channel 6: not used
Channel 7: SirMorph short distance IR sensor
CSV creation sample code:
spi_device(1) mul=0.002 csv = time.asctime() # start each csv line with a time stamp for chan in range(0,8): x = readadc(chan) csv = csv + "," + str(x*mul) print chan, x*mul csv = csv + "\n" f = open("csv.txt","a") f.write(csv) f.close()
(Click on analogmux.py to download the source code)
I ran some timing tests, and the Raspberry Pi Model B was able to read approximately 10,000 samples per second using the code above (not printing or saving the values, just reading them).
Results
(Bottom of Data Acquisition board – click on the image for a larger version)
Here is a sample of the CSV file produced by analogmux.py:
Fri Mar 20 12:37:09 2015,3.312,0.006,4.096,4.906,0.006,0.318,0.006,0.022
Fri Mar 20 12:37:27 2015,3.312,0.006,4.096,4.906,0.006,0.164,0.006,0.02
Fri Mar 20 12:37:44 2015,3.312,0.006,4.096,4.896,0.006,0.24,0.006,0.02
Fri Mar 20 12:38:01 2015,3.312,0.006,4.096,4.906,0.006,0.24,0.006,0.02
Fri Mar 20 12:38:17 2015,3.312,0.006,4.096,4.902,0.006,0.238,0.006,0.022
Fri Mar 20 12:38:33 2015,3.312,0.006,4.096,4.906,0.006,0.24,0.006,0.02
Fri Mar 20 12:38:49 2015,3.314,0.006,4.096,4.906,0.006,0.162,0.006,0.022
Fri Mar 20 12:39:04 2015,3.314,0.006,4.096,4.902,0.006,0.16,0.006,0.02
Fri Mar 20 12:39:20 2015,3.312,0.006,4.096,4.908,0.006,0.182,0.006,0.022
Fri Mar 20 12:39:36 2015,3.312,0.006,4.096,4.906,0.006,0.182,0.006,0.02
Fri Mar 20 12:39:53 2015,3.312,0.006,4.096,4.904,0.006,0.18,0.006,0.022
Fri Mar 20 12:40:08 2015,3.312,0.006,4.096,4.904,0.006,0.18,0.006,0.022
Fri Mar 20 12:40:26 2015,3.312,0.006,4.096,4.906,0.006,0.162,0.006,0.02
Fri Mar 20 12:40:42 2015,3.312,0.006,4.096,4.872,0.006,0.336,0.006,0.018
I imported the CSV file into LibreOffice Calc, and with a few mouse clicks generated a nice chart of the Sharp distance sensor data that was recorded while I was working on some code:
(click on the image for a larger version)
Matlab, GNUPlot, SciLab and other software could also be used to generate charts.
Conclusion
I hope you enjoyed this article!
I used the SPI address de-multiplexer for MCP3208 analog to digital converters, but you can actually use the same approach for any mix of eight SPI devices. Just remember to switch to the correct device before communicating with it!
Quite a few of my future articles will feature data gathered by this system, I’ve found it quite handy already, and instead of CSV files, I am planning to save the data in an SQL data base.
Please feel free to post questions about this project on my forums, or on the thread I made for it on the Raspberry Pi forums.
Related Links
- EZasPie 300 prototyping board used in this article
- Raspberry Pi 1602 and 2004 LCD interfacing
- Raspberry Pi I2C 4×4 matrix keypad with MCP23017 and Python
- Elf RoboPi/Raspberry Pi Robot
- Raspberry Pi 2 NAS Experiments & Howto
- Raspberry Pi 2 USB hard drive and adapter tests
- Raspberry Pi 2 Model B Review
- Raspberry Pi 2: Raspbian vs. Linero (ARMv6 vs ARMv7)
- Raspberry Pi Model A+ Review
- Raspberry Pi Model B+ Review
discount levitra no rx It is now become a prominent brand today when the market has hit hundreds of alternatives. If ignored, it can lead to permanent scarring or it pfizer viagra samples may also cause erectile dysfunction. Here some experts suggest that Diuretic and anti-inflammatory pill can also improve our body’s internal organizational environment and inhibit the the cost of viagra growth of bacteria. One of the best advantages of Kamagra is that the use of this medicine should be done only after getting your doctor’s approval and is available in market such as Kamagra, cheapest brand cialis,and a lot more.
Article Index:
- Introduction, Problems: Conversion Error, Number of Channels, Input Voltage Range, Schematic
- Software Support, Sample Code, Results, Conclusion
Pages: 1 2