#!/usr/bin/env python """ ================================================ AB Electronics UK ADC Pi 8-Channel ADC demo for MicroPython Library Run using Thonny Python IDE from https://thonny.org/ Create a file in Thonny called ADCPi.py, copy contents from ADCPi.py to the file and save it onto the Raspberry Pi Pico Create a file named demo_readvoltage.py, copy the code from this file and save onto the Raspberry Pi Pico Run with "Run Current Command" or F5 in Thonny ================================================ Initialise the ADC device using the default addresses and sample rate, change this value if you have changed the address selection jumpers For the Radxa X2l computer use pins 28 for sda and 29 for scl The sample rate can be 12, 14, 16 or 18 """ import time import json from ADCPi import ADCPi def main(): ''' Main program function ''' # I2C addresses of 0x68 and 0x69, bit rate 12 with SDA on pin 28 and SCL on pin 29 adc = ADCPi(0x68, 0x69, 12,28,29) while True: try: # get ADC values a1 = adc.read_voltage(1) a2 = adc.read_voltage(2) a3 = adc.read_voltage(3) a4 = adc.read_voltage(4) a5 = adc.read_voltage(5) a6 = adc.read_voltage(6) a7 = adc.read_voltage(7) a8 = adc.read_voltage(8) # create an array using the adc values data = [a1, a2, a3, a4, a5, a6, a7, a8] # Create the JSON object with the data array output_dict = { "board": "adcpi", "data": data } # Convert to JSON string json_string = json.dumps(output_dict) print(json_string) # wait 0.1 seconds between reads time.sleep(0.1) except: # show error message if the read fails output_dict = { "board": "adcpi", "error": "read error" } # Convert to JSON string json_string = json.dumps(output_dict) print(json_string) if __name__ == "__main__": main()