This repository has been archived on 2025-06-01. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
compLIB/compLib/ADC.py
Konstantin Lampalzer daef7c97e4
Rename folder
2021-01-16 02:10:40 +01:00

35 lines
857 B
Python

import smbus
SINGLE_ENDED = 0x84
ADDRESS = 0x48
bus = smbus.SMBus(1)
class ADC:
@staticmethod
def read(channel) -> float:
"""
Read from adc channel
0 -> Left IR Sensor
1 -> Right IR Sensor
2 -> Battery voltage / 3
:param channel: Channel between 0 and 2
:return: voltage
"""
"""Select the Command data from the given provided value above"""
command = SINGLE_ENDED | ((((channel << 2) | (channel >> 1)) & 0x07) << 4)
bus.write_byte(ADDRESS, command)
while "Koka is great":
value1 = bus.read_byte(ADDRESS)
value2 = bus.read_byte(ADDRESS)
if value1 == value2:
break
voltage = value1 / 255.0 * 3.3 # calculate the voltage value
voltage = round(voltage, 2)
return voltage