====== MCP 3008 ======
Le Raspberry pi est doté des GPIO, capables de servir d’entrées(tuto) ou de sorties(tuto) numériques. On peut donc lire des signaux logiques hauts ou bas (des 0 ou des 1). Il n’est en revanche pas possible de lire directement des valeurs analogiques. Pour cela, il faut utiliser un ADC, pour “Analog to Digital Converter”, ou convertisseur analogique vers numérique en français, dont le Raspberry Pi n’est pas doté. L’objet de ce tutoriel sera justement de connecter une puce, la MCP3008, au Raspberry pi via le bus SPI pour ajouter huit entrées analogiques.
**Ressources** * [[https://www.raspberrypi.org/learning/physical-computing-with-python/analogue/|physical-computing-with-python]] * [[https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008|raspberry-pi-analog-to-digital-converters]] * {{:materiel:mcp3008:mcp3008.pdf|datasheet}} {{:materiel:mcp3008:mcp3008-01.jpg?400|}} ===== Connection ===== Connections SPI (logiciel) : {{ :materiel:mcp3008:screen_shot_10-10-17_at_11.43_am.png?nolink |}} ^ MCP3008 pins ^ Raspberry-pi pins ^ | VDD | 5V ou 3.3v | | VREF | 5V ou 3.3v | | AGND | GND | | DGND | GND | | CLK | gpio12 | | DOUT | gpio16 | | DIN | gpio20 | | CS/SHDN | gpio21 | On peut se brancher sur n'importe quel GPIO du moment que le script python ensuite les prenne en compte. \\ On peut brancher le dispositif a chaud sur le raspberry pi. ===== Installation ===== sudo apt-get update sudo apt-get install build-essential python-pip python-dev python-smbus git sudo pip3 install python-osc **GPIO** git clone https://github.com/adafruit/Adafruit_Python_GPIO.git cd Adafruit_Python_GPIO sudo python3 setup.py install **MCP3008** git clone https://github.com/adafruit/Adafruit_Python_MCP3008.git cd Adafruit_Python_MCP3008 sudo python3 setup.py install ===== Code ===== Code pour recevoir via python dans pdextended les valeurs analogiques du MCP3008 * {{:materiel:mcp3008:mcp_to_osc.tar.gz|}} """ Get Analog sensors from mcp3008 and send to OSC """ import time from pythonosc import udp_client import Adafruit_GPIO.SPI as SPI import Adafruit_MCP3008 # Software SPI configuration: CLK = 12 MISO = 16 MOSI = 20 CS = 21 SECONDS = 0.02 if __name__ == "__main__": mcp = Adafruit_MCP3008.MCP3008(clk=CLK, cs=CS, miso=MISO, mosi=MOSI) client = udp_client.SimpleUDPClient("127.0.0.1", 8000) # Main program loop. while True: # Read all the ADC channel values in a list. values = [0]*8 for i in range(8): # The read_adc function will get the value of the specified channel (0-7). values[i] = mcp.read_adc(i) client.send_message("/ana"+str(i), values[i]) # Pause for half a second. time.sleep(SECONDS)