Differences
This shows you the differences between two versions of the page.
en:configuration_analogic_inputs [2016/01/29 14:00] |
en:configuration_analogic_inputs [2020/07/05 17:13] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Analogs inputs configuration ====== | ||
+ | Le Raspberry Pi (2) est dépourvu d' | ||
+ | <note important> | ||
+ | |||
+ | Il faut dans un premier temps mettre en route le bus SPI au démarrage. | ||
+ | |||
+ | ===== File / | ||
+ | Edit the file / | ||
+ | Run editor with this command | ||
+ | <code bash> | ||
+ | sudo nano / | ||
+ | </ | ||
+ | then add this line in the file at the end of the page | ||
+ | < | ||
+ | dtparam=spi=on | ||
+ | </ | ||
+ | |||
+ | ===== Installation of the python module for the SPI bus management ===== | ||
+ | For software modules installation, | ||
+ | <code bash> | ||
+ | sudo apt-get install python-dev python-pip | ||
+ | sudo pip install spidev | ||
+ | </ | ||
+ | ===== Acquisition tests of input analogs channels ===== | ||
+ | To verify it works, the following python code will allow you to read the values (and display in points and voltage of the 4 inputs). | ||
+ | |||
+ | <code python | test-analog.py> | ||
+ | # | ||
+ | # | ||
+ | # MCP3204/ | ||
+ | # | ||
+ | # how to setup / | ||
+ | # $ sudo modprobe spi_bcm2708 | ||
+ | # | ||
+ | # how to setup spidev | ||
+ | # $ sudo apt-get install python-dev python-pip | ||
+ | # $ sudo pip install spidev | ||
+ | # | ||
+ | import spidev | ||
+ | import time | ||
+ | |||
+ | class MCP3208: | ||
+ | def __init__(self, | ||
+ | self.spi_channel = spi_channel | ||
+ | self.conn = spidev.SpiDev(0, | ||
+ | self.conn.max_speed_hz = 1000000 # 1MHz | ||
+ | |||
+ | def __del__( self ): | ||
+ | self.close | ||
+ | |||
+ | def close(self): | ||
+ | if self.conn != None: | ||
+ | self.conn.close | ||
+ | self.conn = None | ||
+ | |||
+ | def bitstring(self, | ||
+ | s = bin(n)[2:] | ||
+ | return ' | ||
+ | |||
+ | def read(self, adc_channel=0): | ||
+ | # build command | ||
+ | cmd = 128 # start bit | ||
+ | cmd += 64 # single end / diff | ||
+ | if adc_channel % 2 == 1: | ||
+ | cmd += 8 | ||
+ | if (adc_channel/ | ||
+ | cmd += 16 | ||
+ | if (adc_channel/ | ||
+ | cmd += 32 | ||
+ | |||
+ | # send & receive data | ||
+ | reply_bytes = self.conn.xfer2([cmd, | ||
+ | |||
+ | # | ||
+ | reply_bitstring = '' | ||
+ | # print reply_bitstring | ||
+ | |||
+ | # see also... http:// | ||
+ | reply = reply_bitstring[5: | ||
+ | return int(reply, 2) | ||
+ | |||
+ | if __name__ == ' | ||
+ | spi = MCP3208(0) | ||
+ | |||
+ | count = 0 | ||
+ | a0 = 0 | ||
+ | a1 = 0 | ||
+ | a2 = 0 | ||
+ | a3 = 0 | ||
+ | |||
+ | #while count <= 11: | ||
+ | while True: | ||
+ | count += 1 | ||
+ | a0 += spi.read(0) | ||
+ | a1 += spi.read(1) | ||
+ | a2 += spi.read(2) | ||
+ | a3 += spi.read(3) | ||
+ | |||
+ | if count == 10: | ||
+ | print " | ||
+ | time.sleep(1) | ||
+ | |||
+ | count = 0 | ||
+ | a0 = 0 | ||
+ | a1 = 0 | ||
+ | a2 = 0 | ||
+ | a3 = 0 | ||
+ | </ | ||
+ | |||
+ | Here is a test sequence, with the voltage setting of 3.3V (taken on the 1WIRE pin), directly sent to pins 1 to 4 of the MCP3204 (IC7) | ||
+ | {{ : | ||