Skip to content

Instantly share code, notes, and snippets.

@Komet-Kazi
Last active April 14, 2021 19:30
Show Gist options
  • Select an option

  • Save Komet-Kazi/19af0d4be0953d8d7e72bac7c3b29251 to your computer and use it in GitHub Desktop.

Select an option

Save Komet-Kazi/19af0d4be0953d8d7e72bac7c3b29251 to your computer and use it in GitHub Desktop.
Sensors and Circuits
#!/usr/bin/env python3
########################################################################
# Filename : ADCDevice.py
# Description : Freenove ADC Module library.
# Author : www.freenove.com
# modification: 2020/04/21
########################################################################
import smbus
class ADCDevice(object):
def __init__(self):
self.cmd = 0
self.address = 0
self.bus=smbus.SMBus(1)
# print("ADCDevice init")
def detectI2C(self,addr):
try:
self.bus.write_byte(addr,0)
print("Found device in address 0x%x"%(addr))
return True
except:
print("Not found device in address 0x%x"%(addr))
return False
def close(self):
self.bus.close()
class PCF8591(ADCDevice):
def __init__(self):
super(PCF8591, self).__init__()
self.cmd = 0x40 # The default command for PCF8591 is 0x40.
self.address = 0x48 # 0x48 is the default i2c address for PCF8591 Module.
def analogRead(self, chn): # PCF8591 has 4 ADC input pins, chn:0,1,2,3
value = self.bus.read_byte_data(self.address, self.cmd+chn)
value = self.bus.read_byte_data(self.address, self.cmd+chn)
return value
def analogWrite(self,value): # write DAC value
self.bus.write_byte_data(address,cmd,value)
class ADS7830(ADCDevice):
def __init__(self):
super(ADS7830, self).__init__()
self.cmd = 0x84
self.address = 0x4b # 0x4b is the default i2c address for ADS7830 Module.
def analogRead(self, chn): # ADS7830 has 8 ADC input pins, chn:0,1,2,3,4,5,6,7
value = self.bus.read_byte_data(self.address, self.cmd|(((chn<<2 | chn>>1)&0x07)<<4))
return value
import handout # handout: exclude
from pathlib import Path # handout: exclude
OUTPUT = Path(__file__).parent / Path(__file__).with_suffix('') # handout: exclude
doc = handout.Handout(OUTPUT) # handout: exclude
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment