I am making a project for my design class with my Raspberry Pi Model 3 B+ and Sharp Sensors. This project connects sensors that track distances for a car's bumper (like a parking sensor). I am using the Sharp 2Y0A02F sensors. I currently have both of the sensors feeding into a MCP3008 ADC that then go to the GPIO. I can read a sensor's input in small decimal. I'm assuming it is the voltage reading but that's not my concern here. I'm trying to read from channel 0 and 1 of the ADC at the same time so I can have multiple sensors reading data. Can someone assist with how this would be possible?
Code: Select all
from __future__ import division #import spidev for use import spidev from time import sleep def bitstring(n): s = bin(n)[2:] return '0'*(8-len(s)) + s #read from adc and match with spi channel def read(adc_channel=0, spi_channel=0): conn = spidev.SpiDev(0, spi_channel) conn.max_speed_hz = 1350000 # 1.35 MHz conn.mode = 0 cmd = 192 #start bit + single ended if adc_channel: cmd += 32 #set the ODD/SIGN bit to select channel number reply_bytes = conn.xfer2([cmd, 0]) reply_bitstring = ''.join(bitstring(n) for n in reply_bytes) reply = reply_bitstring[5:15] return int(reply, 2) / 2**10 if __name__ == '__main__': while True: print(read()) sleep(1)