In this lesson we show how to detect light levels with a Raspberry Pi and a photoresistor. The photoresistor is connected in a voltage divider to the ADC0834 AD converter, which then connects to the Raspberry Pi. The schematic of the circuit is below:
Tag Archives: ADC0834
Creating a programmable Temperature Alarm with the Raspberry Pi
In this video lesson we show how to create a programmable temperature alarm using the Raspberry Pi, the DHT11 sensor, the ADC0834, a potentiometer and a buzzer. The results are displayed on a LCD1602 LCD display with a i2c connection. The device operates in either program mode or monitor mode. Pressing the button puts you in programming mode. In this mode you turn the potentiometer until your desired set temperature is reached. Then pressing the button again will switch you to monitor mode. In monitor mode the current temperature and humidity are displayed on the LCD. When the temperature exceeds your setpoint, the buzzer will release an audible alarm. Below is the schematic for our build.
Sunfounder ADC0834 Analog to Digital Chip Library for the Raspberry Pi
We will be using the Sunfounder Ultimate Raspberry Pi kit in all these lessons. You can pick your kit up HERE. In order to use the ADC0834 Analog to Digital converter in your projects, you must have the following library installed on your system. In order to do this, you need to copy the code below. Then create a new program on your Raspberry Pi, and paste the code. Then the code should be saved as ‘ADC0834.py’ in the SAME folder you will be running your main python program from. Also, you may save the program in the default python library file on the raspberry pi. To do this, you can save the file to:
/usr/lib/python3.7/ADC0834.py
Note the above includes the path and the file name. Using this is a better option, as any program on python3.7 should find the library here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
#!/usr/bin/env python3 #----------------------------------------------------- # # This is a program for all ADC chip. It # convert analog singnal to digital signal. # # This program is most analog signal modules' # dependency. Use it like this: # `import ADC0834` # `sig = ADC0834.getResult(chn)` # # *'chn' should be 0,1,2,3 represent for ch0, ch1, ch2, ch3 # on ADC0834 # import RPi.GPIO as GPIO import time ADC_CS = 17 ADC_CLK = 18 ADC_DIO = 27 # using default pins for backwards compatibility def setup(cs=17,clk=18,dio=27): global ADC_CS, ADC_CLK, ADC_DIO ADC_CS=cs ADC_CLK=clk ADC_DIO=dio GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) # Number GPIOs by BCM mode GPIO.setup(ADC_CS, GPIO.OUT) # Set pins' mode is output GPIO.setup(ADC_CLK, GPIO.OUT) # Set pins' mode is output def destroy(): GPIO.cleanup() # using channel = 0 as default for backwards compatibility def getResult(channel=0): # Get ADC result, input channel sel = int(channel > 1 & 1) odd = channel & 1 # print("sel: {}, odd: {}".format(sel, odd)) GPIO.setup(ADC_DIO, GPIO.OUT) GPIO.output(ADC_CS, 0) # Start bit GPIO.output(ADC_CLK, 0) GPIO.output(ADC_DIO, 1) time.sleep(0.000002) GPIO.output(ADC_CLK, 1) time.sleep(0.000002) # Single End mode GPIO.output(ADC_CLK, 0) GPIO.output(ADC_DIO, 1) time.sleep(0.000002) GPIO.output(ADC_CLK, 1) time.sleep(0.000002) # ODD GPIO.output(ADC_CLK, 0) GPIO.output(ADC_DIO, odd) time.sleep(0.000002) GPIO.output(ADC_CLK, 1) time.sleep(0.000002) # Select GPIO.output(ADC_CLK, 0) GPIO.output(ADC_DIO, sel) time.sleep(0.000002) GPIO.output(ADC_CLK, 1) time.sleep(0.000002) GPIO.output(ADC_CLK, 0) time.sleep(0.000002) # ODD # GPIO.output(ADC_CLK, 0) # GPIO.output(ADC_DIO, channel) # time.sleep(0.000002) # GPIO.output(ADC_CLK, 1) # GPIO.output(ADC_DIO, 1) # time.sleep(0.000002) # GPIO.output(ADC_CLK, 0) # GPIO.output(ADC_DIO, 1) # time.sleep(0.000002) dat1 = 0 for i in range(0, 8): GPIO.output(ADC_CLK, 1); time.sleep(0.000002) GPIO.output(ADC_CLK, 0); time.sleep(0.000002) GPIO.setup(ADC_DIO, GPIO.IN) dat1 = dat1 << 1 | GPIO.input(ADC_DIO) dat2 = 0 for i in range(0, 8): dat2 = dat2 | GPIO.input(ADC_DIO) << i GPIO.output(ADC_CLK, 1); time.sleep(0.000002) GPIO.output(ADC_CLK, 0); time.sleep(0.000002) GPIO.output(ADC_CS, 1) GPIO.setup(ADC_DIO, GPIO.OUT) if dat1 == dat2: return dat1 else: return 0 def getResult1(): return getResult(1) def loop(): while True: for i in range(4): res = getResult(i) print ('res{} = {}'.format(i,res)) time.sleep(0.1) time.sleep(1) if __name__ == '__main__': # Program start from here setup() try: loop() except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed. destroy() |