MCP3004/3008, 2.7V 4-Channel/8-Channel 10-Bit A/D Converters with SPI Serial Interface (Raspberry Pi)

Used Products:

1. MCP3008
2. Raspberry Pi 3 Model B

Content:

Raspberry Pi 3 Enable spi 1. Run this command: sudo nano /boot/config.txt 2. Add this at the end of the file: dtparam=spi=on 3. Save file pressing: CTRL+X, Y and Enter 4. Reboot your system: sudo reboot Çeşme: https://raspberrypi.stackexchange.com/questions/48228/how-to-enable-spi-on-raspberry-pi-3 Çeşme --------------- 1. https://www.youtube.com/watch?v=LW95W7kvkEM 2. https://www.youtube.com/watch?v=wIAffBTtrKY 3. https://www.youtube.com/watch?v=Yevjk2qkqlw

Code:

MCP3008.py ----------------------------- import spidev from time import sleep # First open up SPI bus spi = spidev.SpiDev() spi.open(0,0) # Initialize what sensor is where lightChannel = 0; tempChannel = 1; sleepTime = 1; def getReading(channel): # First pull the raw data from the chip rawData = spi.xfer([1, (8 + channel) << 4, 0]); # Process the raw data into something we understand processedData = ((rawData[1]&3) << 8) + rawData[2] return processedData; while True: data = getReading(lightChannel) print(data); sleep(sleepTime)