It is incredibly easy to get a simple ADC able to read sensor devices whose resistance is changed by some physical property- with just a pair of components. The image shows a continuous display of the sensor in a GUI window.
All you need is say a LDR from +3.3V in series with a capacitor of say 0.1uF to earth. The junction goes to Pi pin 18.
A simple Python3 program discharges the capacitor, then counts up as it recharges, saving the count when it passes the CPU GPIO input threshold voltage. I chose to average the new reading with the previous one, to provide some smoothing but still a reasonably fast response.
The rest of the program just creates a Tkinter GUI display and calls the read routine at regular intervals.
You could of course instead save the values to a csv file for analysis; or display graphically on screen. Programming this helped me understand, among other things-Pythonesque, the ideas of operator overloading and of string-slicing and overlooading. If sent to a fan-fold old-fashined printer this makes a very impressive chart recorder!!
/usr/bin/env python3 <ctrl><C> to end #sudo python analogPortViaRC_GUI_a.py to run, or run 'sudo idle3' and load first... # Assumes a LDR from +3.3V in series with capacitor of 0.1uF to ground. Junction to pin 18. import time, os import RPi.GPIO as GPIO from tkinter import * root =Tk() root.title( "Cheapo-ADC") meter =Label( root, font =('arial', 36, 'bold'), bg ='green') meter.pack( fill=BOTH, expand=1) GPIO.setwarnings( False) GPIO.setmode( GPIO.BCM) # we want to use the Pi connector's pin numbering scheme GPIO.setup( 18, GPIO.OUT) # setup pin 18 def RCtime( RCpin): # read cycles taken to come back above threshold voltage reading =0 GPIO.setup( RCpin, GPIO.OUT) GPIO.output( RCpin, GPIO.LOW) # ...after first being pulled low & discharged. time.sleep( 0.1) GPIO.setup( RCpin, GPIO.IN) while ( GPIO.input( RCpin) ==GPIO.LOW): reading +=1 return reading def measure(): # smooth data by loaded average of new and previous reading. global runAve sensor_value =RCtime( 18) runAve =0.8 *runAve +0.2 *sensor_value meter.config( text ='%4.1f' % runAve) # format to 1 d.p. meter.after( 1000, measure) # re-do after 1 second. runAve =RCtime( 18) # get an initial value measure() root.mainloop() #GPIO.cleanup()
#!/usr/bin/env python #<ctrl><C> to end #sudo python analogPortViaRC5.py to run, or run 'sudo idle3' and load first... import time, os, time import RPi.GPIO as GPIO GPIO.setwarnings( False) # we want to use the Pi connector's pin numbering scheme GPIO.setmode( GPIO.BCM) # setup pin 18 GPIO.setup( 18, GPIO.OUT) def RCtime ( RCpin): reading =0 GPIO.setup( RCpin, GPIO.OUT) GPIO.output( RCpin, GPIO.LOW) time.sleep( 0.1) GPIO.setup( RCpin, GPIO.IN) while ( GPIO.input( RCpin) ==GPIO.LOW): reading +=1 return reading bar ="#" #runAverage10 =RCtime( 18) start = time.time() print( "Displaying Analogue input on pin 18.") while True: # read cycles taken to come back above threshold voltage z =( bar *int( RCtime( 18) /10), int( (time.time() - start)), " seconds") print( z) myfile =open( 'tempLog.csv', 'a') myfile.write( str( RCtime( 18))) myfile.write( ',') myfile.write( str( int( (time.time() - start)))) myfile.write( '\n') myfile.close() time.sleep( 5) GPIO.cleanup()