├── README.md ├── adc_voltmeter_16x2.py ├── adc_voltmeter_20x4.py ├── i2c_lib.py ├── lcd_demo16x2.py ├── lcd_demo20x4.py ├── lcddriver.py ├── ldr-led.py ├── ldr.py ├── weather_16x2.py └── weather_20x4.py /README.md: -------------------------------------------------------------------------------- 1 | # RasPiO Analog Zero 2 | 3 | ![alt text](http://raspi.tv/wp-content/uploads/2016/05/RasPiO-Analog-Zero-NOT-on-Pi-Zero-cleaned-copy_1500.jpg "RasPiO Analog Zero") 4 | 5 | 6 | If you don't have one yet, you can buy a 7 | [RasPiO Analog Zero here](http://rasp.io/analogzero "RasPiO Analog Zero") 8 | 9 | 10 | ##License info 11 | Sofware licensed under 12 | [CC-BY-NC-SA 3.0 license](https://creativecommons.org/licenses/by-nc-sa/3.0/) 13 | 14 | 15 | ##Setup 16 | 17 | ###Basic Use 18 | No setup is required. Just solder up your board, download the 19 | [user guide here](http://rasp.io/analogzero "RasPiO Analog Zero User Guide") 20 | and start playing with GPIO Zero. 21 | 22 | ###Project Kits and i2c LCDs 23 | 24 | You need to have i2c activated on your Pi and you also need smbus installed. 25 | 26 | You will also need these two Python driver files... 27 | 28 | lcddriver.py 29 | 30 | i2c_lib 31 | 32 | in the same directory where you're putting your LCD control scripts. 33 | 34 | It's all covered in depth in the 35 | [user guide here](http://rasp.io/analogzero "RasPiO Analog Zero User Guide") 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /adc_voltmeter_16x2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | from time import sleep 3 | from gpiozero import MCP3008 4 | import lcddriver 5 | vref = 3.296 6 | adc_list = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0] 7 | conversion_factors = [3.080,4.668,6.43,1,1,1,1,1] 8 | toggle = 0 9 | counter = 0 10 | lcd = lcddriver.lcd() # create object for lcd control 11 | lcd.lcd_clear() # clear LCD ready for start 12 | 13 | def update(): 14 | lcd.lcd_display_string('{:^16}'.format(row_one), 1) 15 | lcd.lcd_display_string('{:^16}'.format(row_two), 2) 16 | 17 | # display an intro message 18 | row_one = "Hi 16x2 RasPiO" 19 | row_two = "Analog Zero" 20 | update() 21 | sleep(2) 22 | 23 | while True: 24 | for x in range(3): 25 | adc = MCP3008(channel=x) 26 | readings = 0.0 27 | repetitions = 200 # how many times we sample 28 | for y in range(repetitions): 29 | readings += adc.value 30 | average = readings / repetitions 31 | volts = '{:6.3f}'.format(vref * average * conversion_factors[x]) 32 | print("channel " + str(x) + ":", volts,"Volts") 33 | adc_list[x] = volts 34 | if x == 2: 35 | row_one = str("10V:"+adc_list[0])+"V" 36 | row_two = str("15V:"+adc_list[1])+"V" 37 | row_three = str("20V:"+adc_list[2])+"V" 38 | row_four = str("RasPiO Voltmeter") 39 | if counter % 7 == 0: # change to tweak alternation 40 | if toggle == 0: 41 | toggle = 1 42 | elif toggle == 1: 43 | toggle = 0 44 | if toggle: 45 | row_one = row_three 46 | row_two = row_four 47 | update() 48 | sleep(0.05) 49 | counter += 1 50 | 51 | # Calibration 52 | # The voltmeter will be more or less accurate (~5%) as is, but 53 | # if you want accurate results, measure your Pi's 3V3 line with 54 | # an accurate Voltmeter and adjust the value of vref in line 5 55 | # Then set conversion_factors = [1,1,1,1,1,1,1,1] in line 7 56 | # Then run the script and connect each of 57 | # 10V (ch0), 15V (ch1) and 20V (ch2) channels to Pi's 3V3 58 | # your conversion factors are... 59 | # vref / measured value 60 | # then edit conversion_factors to include your values to 3 dp 61 | -------------------------------------------------------------------------------- /adc_voltmeter_20x4.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | from time import sleep 3 | from gpiozero import MCP3008 4 | import lcddriver 5 | vref = 3.296 6 | adc_list = [0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0] 7 | conversion_factors = [3.080,4.668,6.43,1,1,1,1,1] 8 | lcd = lcddriver.lcd() # create object for lcd control 9 | lcd.lcd_clear() # clear LCD ready for start 10 | 11 | def update(): 12 | lcd.lcd_display_string('{:^20}'.format(row_one), 1) 13 | lcd.lcd_display_string('{:^20}'.format(row_two), 2) 14 | lcd.lcd_display_string('{:^20}'.format(row_three), 3) 15 | lcd.lcd_display_string('{:^20}'.format(row_four), 4) 16 | 17 | # display an intro message 18 | row_one = "Hi 20x4 RasPiO" 19 | row_two = "Analog Zero" 20 | row_three = "Multi-range" 21 | row_four = "Voltmeter" 22 | update() 23 | sleep(2) 24 | 25 | while True: 26 | for x in range(3): 27 | adc = MCP3008(channel=x) 28 | readings = 0.0 29 | repetitions = 200 # how many times we sample 30 | for y in range(repetitions): 31 | readings += adc.value 32 | average = readings / repetitions 33 | volts = '{:6.3f}'.format(vref * average * conversion_factors[x]) 34 | print("channel " + str(x) + ":", volts,"Volts") 35 | adc_list[x] = volts 36 | if x == 2: 37 | row_one = str("10V range:"+adc_list[0])+"V" 38 | row_two = str("15V range:"+adc_list[1])+"V" 39 | row_three = str("20V range:"+adc_list[2])+"V" 40 | row_four = str("RasPiO Analog Zero") 41 | update() 42 | sleep(0.05) 43 | 44 | # Calibration 45 | # The voltmeter will be more or less accurate (~5%) as is, but 46 | # if you want accurate results, measure your Pi's 3V3 line with 47 | # an accurate Voltmeter and adjust the value of vref in line 5 48 | # Then set conversion_factors = [1,1,1,1,1,1,1,1] in line 7 49 | # Then run the script and connect each of 50 | # 10V (ch0), 15V (ch1) and 20V (ch2) channels to Pi's 3V3 51 | # your conversion factors are... 52 | # vref / measured value 53 | # then edit conversion_factors to include your values to 3 dp 54 | 55 | 56 | # If you want to display all 8 channels, swap out these lines for 37-40 57 | # and change 3 in line 26 "x in range(3)" to 7 58 | # row_one = str("0:"+adc_list[0])+"V 4:"+str(adc_list[4])+"V " 59 | # row_two = str("1:"+adc_list[1])+"V 5:"+str(adc_list[5])+"V " 60 | # row_three = str("2:"+adc_list[2])+"V 6:"+str(adc_list[6])+"V " 61 | # row_four = str("3:"+adc_list[3])+"V 7:"+str(adc_list[7])+"V " 62 | -------------------------------------------------------------------------------- /i2c_lib.py: -------------------------------------------------------------------------------- 1 | import smbus 2 | from time import * 3 | 4 | class i2c_device: 5 | def __init__(self, addr, port=1): 6 | self.addr = addr 7 | self.bus = smbus.SMBus(port) 8 | 9 | # Write a single command 10 | def write_cmd(self, cmd): 11 | self.bus.write_byte(self.addr, cmd) 12 | sleep(0.0001) 13 | 14 | # Write a command and argument 15 | def write_cmd_arg(self, cmd, data): 16 | self.bus.write_byte_data(self.addr, cmd, data) 17 | sleep(0.0001) 18 | 19 | # Write a block of data 20 | def write_block_data(self, cmd, data): 21 | self.bus.write_block_data(self.addr, cmd, data) 22 | sleep(0.0001) 23 | 24 | # Read a single byte 25 | def read(self): 26 | return self.bus.read_byte(self.addr) 27 | 28 | # Read 29 | def read_data(self, cmd): 30 | return self.bus.read_byte_data(self.addr, cmd) 31 | 32 | # Read a block of data 33 | def read_block_data(self, cmd): 34 | return self.bus.read_block_data(self.addr, cmd) 35 | -------------------------------------------------------------------------------- /lcd_demo16x2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | import lcddriver 3 | from time import sleep, strftime 4 | from datetime import datetime 5 | 6 | # LCD custom character variables 7 | degree = chr(0) 8 | squared = chr(1) 9 | cust_chars = [[0x1c,0x14,0x1c,0x0,0x0,0x0,0x0,0x0], # degree 10 | [0x8,0x14,0x8,0x10,0x1c,0x0,0x0,0x0]] # squared 11 | 12 | lcd = lcddriver.lcd() # create object for lcd control 13 | lcd.lcd_load_custom_chars(cust_chars) # upload custom chars to LCD 14 | lcd.lcd_clear() # clear LCD ready for start 15 | 16 | # display an intro message 17 | lcd.lcd_display_string('{:^16}'.format("RasPiO Analog Zero"), 1) 18 | lcd.lcd_display_string('{:^16}'.format("16x2 Weather Kit"), 2) 19 | 20 | sleep(3) 21 | lcd.lcd_clear() 22 | 23 | def update(): 24 | lcd.lcd_display_string('{:^16}'.format(row_one), 1) 25 | lcd.lcd_display_string('{:^16}'.format(row_two), 2) 26 | 27 | # display a range of keyboard characters 28 | lcd.lcd_display_string("ABCDEFGHIJKLMNOPQRST", 1) 29 | lcd.lcd_display_string("UVWXYZabcdefghijklmn", 2) 30 | sleep(1) 31 | 32 | # activate every single pixel to test the display 33 | pixel_test = chr(255) * 16 34 | for x in range(1,3): 35 | lcd.lcd_display_string(pixel_test, x) 36 | sleep(1) 37 | 38 | row_one = "Make Custom Chars" 39 | row_two = "on i"+squared+"c LCD "+degree+"C" 40 | update() 41 | sleep(3) 42 | 43 | lcd.lcd_display_string('{:<16}'.format("align left"), 1) 44 | lcd.lcd_display_string('{:>16}'.format("align right"), 2) 45 | sleep(3) 46 | 47 | for x in range(1,3): 48 | lcd.lcd_display_string('{:^16}'.format("center"), x) 49 | sleep(2) 50 | 51 | try: # now we're ready to start the main loop 52 | lcd.lcd_clear() 53 | 54 | while True: # correct time needs internet or RTC 55 | timenow = datetime.now().strftime('%b %d %H:%M:%S') 56 | row_one = timenow 57 | row_two = "RasPiO Analog Zero" 58 | update() 59 | sleep(0.1) 60 | 61 | finally: 62 | for x in range(5): 63 | timenow = datetime.now().strftime('%b %d %H:%M:%S') 64 | row_one = timenow 65 | row_two = "switching off " + str(5-x) +"s" 66 | update() 67 | sleep(1) 68 | lcd.backlight(0) # swap 0 for 1 turns backlight on -------------------------------------------------------------------------------- /lcd_demo20x4.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | import lcddriver 3 | from time import sleep, strftime 4 | from datetime import datetime 5 | 6 | # LCD custom character variables 7 | degree = chr(0) 8 | squared = chr(1) 9 | cust_chars = [[0x1c,0x14,0x1c,0x0,0x0,0x0,0x0,0x0], # degree 10 | [0x8,0x14,0x8,0x10,0x1c,0x0,0x0,0x0]] # squared 11 | 12 | lcd = lcddriver.lcd() # create object for lcd control 13 | lcd.lcd_load_custom_chars(cust_chars) # upload custom chars to LCD 14 | lcd.lcd_clear() # clear LCD ready for start 15 | 16 | # display an intro message 17 | lcd.lcd_display_string('{:^20}'.format("RasPiO Analog Zero"), 1) 18 | lcd.lcd_display_string('{:^20}'.format("20x4 Weather Kit"), 2) 19 | lcd.lcd_display_string('{:^20}'.format("Digi Thermometer"), 3) 20 | lcd.lcd_display_string('{:^20}'.format("Voltmeter"), 4) 21 | sleep(3) 22 | lcd.lcd_clear() 23 | 24 | def update(): 25 | lcd.lcd_display_string('{:^20}'.format(row_one), 1) 26 | lcd.lcd_display_string('{:^20}'.format(row_two), 2) 27 | lcd.lcd_display_string('{:^20}'.format(row_three), 3) 28 | lcd.lcd_display_string('{:^20}'.format(row_four), 4) 29 | 30 | # display a range of keyboard characters 31 | lcd.lcd_display_string("ABCDEFGHIJKLMNOPQRST", 1) 32 | lcd.lcd_display_string("UVWXYZabcdefghijklmn", 2) 33 | lcd.lcd_display_string("opqrstuvwxyz{}[]'|?/", 3) 34 | lcd.lcd_display_string("0123456789!@$%^&*()", 4) 35 | sleep(1) 36 | 37 | # activate every single pixel to test the display 38 | pixel_test = chr(255) * 20 39 | for x in range(1,5): 40 | lcd.lcd_display_string(pixel_test, x) 41 | sleep(1) 42 | 43 | row_one = "Making Custom Chars" 44 | row_two = "on i"+squared+"c LCD "+degree+"C" 45 | row_three = "is quite doable" 46 | row_four = "if you need them" 47 | update() 48 | sleep(3) 49 | 50 | lcd.lcd_display_string('{:<20}'.format("align left"), 1) 51 | lcd.lcd_display_string('{:>20}'.format("align right"), 2) 52 | lcd.lcd_display_string('{:<20}'.format("align left"), 3) 53 | lcd.lcd_display_string('{:>20}'.format("align right"), 4) 54 | sleep(3) 55 | 56 | for x in range(1,5): 57 | lcd.lcd_display_string('{:^20}'.format("center"), x) 58 | sleep(2) 59 | 60 | try: # now we're ready to start the main loop 61 | lcd.lcd_clear() 62 | 63 | while True: # correct time needs internet or RTC 64 | timenow = datetime.now().strftime('%b %d %H:%M:%S') 65 | row_one = timenow 66 | row_two = "RasPiO Analog Zero" 67 | row_three = "20x4 LCD Weather" 68 | row_four = "St'n/Therm/Voltmeter" 69 | update() 70 | sleep(0.1) 71 | 72 | finally: 73 | for x in range(5): 74 | timenow = datetime.now().strftime('%b %d %H:%M:%S') 75 | row_one = timenow 76 | row_two = "Backlight is" 77 | row_three = "switching off in " + str(5-x) +"s" 78 | row_four = "Bye" 79 | update() 80 | sleep(1) 81 | lcd.backlight(0) # swap 0 for 1 turns backlight on -------------------------------------------------------------------------------- /lcddriver.py: -------------------------------------------------------------------------------- 1 | # source https://www.raspberrypi.org/forums/viewtopic.php?p=291313 2 | # tweaked and amended/appended 3 | import i2c_lib 4 | from time import * 5 | 6 | # LCD Address 7 | ADDRESS = 0x27 8 | 9 | # commands 10 | LCD_CLEARDISPLAY = 0x01 11 | LCD_RETURNHOME = 0x02 12 | LCD_ENTRYMODESET = 0x04 13 | LCD_DISPLAYCONTROL = 0x08 14 | LCD_CURSORSHIFT = 0x10 15 | LCD_FUNCTIONSET = 0x20 16 | LCD_SETCGRAMADDR = 0x40 17 | LCD_SETDDRAMADDR = 0x80 18 | 19 | # flags for display entry mode 20 | LCD_ENTRYRIGHT = 0x00 21 | LCD_ENTRYLEFT = 0x02 22 | LCD_ENTRYSHIFTINCREMENT = 0x01 23 | LCD_ENTRYSHIFTDECREMENT = 0x00 24 | 25 | # flags for display on/off control 26 | LCD_DISPLAYON = 0x04 27 | LCD_DISPLAYOFF = 0x00 28 | LCD_CURSORON = 0x02 29 | LCD_CURSOROFF = 0x00 30 | LCD_BLINKON = 0x01 31 | LCD_BLINKOFF = 0x00 32 | 33 | # flags for display/cursor shift 34 | LCD_DISPLAYMOVE = 0x08 35 | LCD_CURSORMOVE = 0x00 36 | LCD_MOVERIGHT = 0x04 37 | LCD_MOVELEFT = 0x00 38 | 39 | # flags for function set 40 | LCD_8BITMODE = 0x10 41 | LCD_4BITMODE = 0x00 42 | LCD_2LINE = 0x08 43 | LCD_1LINE = 0x00 44 | LCD_5x10DOTS = 0x04 45 | LCD_5x8DOTS = 0x00 46 | 47 | # flags for backlight control 48 | LCD_BACKLIGHT = 0x08 49 | LCD_NOBACKLIGHT = 0x00 50 | 51 | En = 0b00000100 # Enable bit 52 | Rw = 0b00000010 # Read/Write bit 53 | Rs = 0b00000001 # Register select bit 54 | 55 | class lcd: 56 | #initializes objects and lcd 57 | def __init__(self): 58 | self.lcd_device = i2c_lib.i2c_device(ADDRESS) 59 | 60 | self.lcd_write(0x03) 61 | self.lcd_write(0x03) 62 | self.lcd_write(0x03) 63 | self.lcd_write(0x02) 64 | 65 | self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE) 66 | self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON) 67 | self.lcd_write(LCD_CLEARDISPLAY) 68 | self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT) 69 | sleep(0.2) 70 | 71 | # clocks EN to latch command 72 | def lcd_strobe(self, data): 73 | self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT) 74 | sleep(.0005) 75 | self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT)) 76 | sleep(.0001) 77 | 78 | def lcd_write_four_bits(self, data): 79 | self.lcd_device.write_cmd(data | LCD_BACKLIGHT) 80 | self.lcd_strobe(data) 81 | 82 | # write a command to lcd 83 | def lcd_write(self, cmd, mode=0): 84 | self.lcd_write_four_bits(mode | (cmd & 0xF0)) 85 | self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0)) 86 | 87 | # put string function 88 | def lcd_display_string(self, string, line): 89 | if line == 1: 90 | self.lcd_write(0x80) 91 | if line == 2: 92 | self.lcd_write(0xC0) 93 | if line == 3: 94 | self.lcd_write(0x94) 95 | if line == 4: 96 | self.lcd_write(0xD4) 97 | 98 | for char in string: 99 | self.lcd_write(ord(char), Rs) 100 | 101 | # clear lcd and set to home 102 | def lcd_clear(self): 103 | self.lcd_write(LCD_CLEARDISPLAY) 104 | self.lcd_write(LCD_RETURNHOME) 105 | 106 | 107 | def backlight(self, state): # for state, 1 = on, 0 = off 108 | if state == 1: 109 | self.lcd_device.write_cmd(LCD_BACKLIGHT) 110 | elif state == 0: 111 | self.lcd_device.write_cmd(LCD_NOBACKLIGHT) 112 | 113 | # add custom characters (0 - 7) 114 | def lcd_load_custom_chars(self, charsdata): 115 | self.lcd_write(0x40) 116 | for char in charsdata: 117 | for line in char: 118 | self.lcd_write_char(line) 119 | 120 | # write a character to lcd (or character rom) 0x09: backlight | RS=DR< 121 | def lcd_write_char(self, charvalue, mode=1): 122 | self.lcd_write_four_bits(mode | (charvalue & 0xF0)) 123 | self.lcd_write_four_bits(mode | ((charvalue << 4) & 0xF0)) 124 | 125 | # write a character to a specific position 126 | def lcd_display_string_pos(self, string, line, pos): 127 | if line == 1: 128 | pos_new = pos 129 | elif line == 2: 130 | pos_new = 0x40 + pos 131 | elif line == 3: 132 | pos_new = 0x14 + pos 133 | elif line == 4: 134 | pos_new = 0x54 + pos 135 | 136 | self.lcd_write(0x80 + pos_new) 137 | 138 | for char in string: 139 | self.lcd_write(ord(char), Rs) 140 | -------------------------------------------------------------------------------- /ldr-led.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | from gpiozero import MCP3008, LED 3 | from time import sleep 4 | red = LED(12) 5 | ldr = MCP3008(channel=7) 6 | 7 | while True: 8 | print("LDR: ", ldr.value) 9 | if ldr.value < 0.5: 10 | red.on() 11 | print ("LED on") 12 | else: 13 | red.off() 14 | print ("LED off") 15 | sleep(0.1) -------------------------------------------------------------------------------- /ldr.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | from gpiozero import MCP3008 3 | from time import sleep 4 | 5 | while True: 6 | for x in range(0, 8): 7 | with MCP3008(channel=x) as reading: 8 | print(x,": ", reading.value) 9 | sleep(0.1) -------------------------------------------------------------------------------- /weather_16x2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | from time import sleep 3 | from gpiozero import MCP3008 4 | import lcddriver 5 | vref = 3.3 6 | channels = [0,0,0,0,0,0,0,0] 7 | temperatures = [0,0] 8 | light_levels = [0,0] 9 | count = 0 10 | 11 | lcd = lcddriver.lcd() # create object for lcd control 12 | lcd.lcd_clear() # clear LCD ready for start 13 | 14 | def update(): 15 | lcd.lcd_display_string('{:^16}'.format(row_one), 1) 16 | lcd.lcd_display_string('{:^16}'.format(row_two), 2) 17 | 18 | # display a centered intro message 19 | row_one = '{:^16}'.format("RasPiO Analog Zero") 20 | row_two = '{:^16}'.format("16x2 Weather Kit") 21 | update() 22 | sleep(3) 23 | 24 | # Wiring instructions 25 | # 26 | # Wire up 2x TMP36 so the middle pins go to A0 and A1 27 | # Wire up 2x LDR. One leg to 3V3 other leg to one end 28 | # of 10k resistor AND A6 or A7. 29 | # Other end of 10k resistor goes to GND 30 | 31 | while True: 32 | for x in range(8): 33 | adc = MCP3008(channel=x) 34 | volts = 0.0 35 | for y in range(20): 36 | volts = volts + (vref * adc.value) 37 | volts = volts / 20.0 38 | if x < 2: 39 | temperatures[x] = '{:4.1f}'.format((volts - 0.5) * 100) 40 | if x > 5: 41 | light_levels[x-6] = '{:4.1f}'.format(volts / vref * 100) 42 | volts = '{:.3f}'.format(volts) 43 | channels[x] = volts 44 | 45 | # on-screen output useful for debug when tweaking 46 | # shows the actual voltage at each analog input 47 | print("channel " + str(x) + ":", volts,"Volts") 48 | 49 | # update the character LCD once every cycle then a short delay 50 | # because we have limited characters, we alternate display 51 | if x == 7: 52 | if count % 2 == 0: 53 | row_one = "Temp 0: " + temperatures[0] + "C" 54 | row_two = "Temp 1: " + temperatures[1] + "C" 55 | else: 56 | row_one = "Light 0: " + light_levels[0] + "%" 57 | row_two = "Light 1: " + light_levels[1] + "%" 58 | update() 59 | count += 1 60 | sleep(0.2) # you can adjust refresh rate here 61 | -------------------------------------------------------------------------------- /weather_20x4.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | from time import sleep 3 | from gpiozero import MCP3008 4 | import lcddriver 5 | vref = 3.3 6 | channels = [0,0,0,0,0,0,0,0] 7 | temperatures = [0,0] 8 | light_levels = [0,0] 9 | 10 | lcd = lcddriver.lcd() # create object for lcd control 11 | lcd.lcd_clear() # clear LCD ready for start 12 | 13 | def update(): 14 | lcd.lcd_display_string('{:^20}'.format(row_one), 1) 15 | lcd.lcd_display_string('{:^20}'.format(row_two), 2) 16 | lcd.lcd_display_string('{:^20}'.format(row_three), 3) 17 | lcd.lcd_display_string('{:^20}'.format(row_four), 4) 18 | 19 | # display a centered intro message 20 | row_one = '{:^20}'.format("Hi. Enjoy using your") 21 | row_two = '{:^20}'.format("RasPiO Analog Zero") 22 | row_three = '{:^20}'.format("20x4 Weather Station") 23 | row_four = '{:^20}'.format("Volt/thermometer Kit") 24 | update() 25 | sleep(3) 26 | 27 | # Wiring instructions 28 | # 29 | # Wire up 2x TMP36 so the middle pins go to A0 and A1 30 | # Wire up 2x LDR. One leg to 3V3 other leg to one end 31 | # of 10k resistor AND A6 or A7. 32 | # Other end of 10k resistor goes to GND 33 | 34 | while True: 35 | for x in range(8): 36 | adc = MCP3008(channel=x) 37 | volts = 0.0 38 | for y in range(20): 39 | volts = volts + (vref * adc.value) 40 | volts = volts / 20.0 41 | if x < 2: 42 | temperatures[x] = '{:4.1f}'.format((volts - 0.5) * 100) 43 | if x > 5: 44 | light_levels[x-6] = '{:4.1f}'.format(volts / vref * 100) 45 | volts = '{:.3f}'.format(volts) 46 | channels[x] = volts 47 | 48 | # on-screen output useful for debug when tweaking 49 | # shows the actual voltage at each analog input 50 | print("channel " + str(x) + ":", volts,"Volts") 51 | 52 | # update the character LCD once every cycle then a short delay 53 | if x == 7: 54 | row_one = "T0: " + temperatures[0] + "C" 55 | row_two = "T1: " + temperatures[1] + "C" 56 | row_three = "L0: " + light_levels[0] + "%" 57 | row_four = "L1: " + light_levels[1] + "%" 58 | update() 59 | sleep(0.1) # you can adjust refresh rate here --------------------------------------------------------------------------------