How to do it...

In the next section, we shall write a script to allow us to gather data that we will then use later on in this chapter.

Create the following script, data_adc.py, as follows:

  1. First, import the modules and create the variables we will use, as follows:
#!/usr/bin/env python3 
#data_adc.py 
import wiringpi2 
import time 
 
DEBUG=False 
LIGHT=0;TEMP=1;EXT=2;POT=3 
ADC_CH=[LIGHT,TEMP,EXT,POT] 
ADC_ADR=0x48 
ADC_CYCLE=0x04 
BUS_GAP=0.25 
DATANAME=["0:Light","1:Temperature", 
          "2:External","3:Potentiometer"] 
  1. Create the device class with a constructor to initialize it, as follows:
class device: # Constructor: def __init__(self,addr=ADC_ADR): self.NAME = DATANAME self.i2c = wiringpi2.I2C() self.devADC=self.i2c.setup(addr) pwrup = self.i2c.read(self.devADC) ...

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.