adventures with microcomputers prototyping with raspberry pi

13. Analog Sensors

reference

equipment

pre-flight

First make sure that i2c has already been enabled for your raspberry pi:

  1. open the config file: sudo raspi-config
  2. using the arrow keys navigate to Interfacing Options, hit ENTER
  3. using the arrow keys navigate to I2C, hit ENTER
  4. select YES, hit ENTER
  5. hit ENTER for OK
  6. hit the right arrow key twice, selecting FINISH, hit ENTER
  7. reboot with sudo reboot now

installation

  1. on your Raspberry enter the following command to clone the ADC library we will use today: git clone https://github.com/adafruit/Adafruit_Python_ADS1x15.git
  2. once it is done downloading cd into the repo: cd Adafruit_Python_ADS1x15
  3. hit ls to see what is in that directory. right now we are looking for a file called setup.py
  4. install the library so python can use it: sudo python3 setup.py install
  5. when your command prompt returns move on to the next section

wiring

Connect the ADC to the Pi as follows:

  • ADS1x15 VDD to Raspberry Pi 3.3V
  • ADS1x15 GND to Raspberry Pi GND
  • ADS1x15 SCL to Raspberry Pi SCL
  • ADS1x15 SDA to Raspberry Pi SDA

Run the following command to check to make sure the Pi sees your ADC: sudo i2cdetect -y 1

You should see something like this:

0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

where 48 denotes my i2c ADC. All you really need to understand here is if you only see dashes, and no numbers, that means the PI cannot see your i2c ADC.

test

  1. on your Pi cd into Adafruit_Python_ADS1x15/examples/ to find the example we will test with: cd Adafruit_Python_ADS1x15/examples
  2. hit ls to locate our test file, simpletest.py
  3. check to see whether or not it is already executable: ls -la
  4. make it executable: sudo chmod +x simpletest.py
  5. hook a sensor up to the A0 pin on your ADC
  6. run the file: sudo ipython3 simpletest.py
  7. you should see the number values in the first column increase/decrease as you interact with the sensor