from machine import Pin, ADC, Timer
led_0 = Pin(27, Pin.OUT)
led_1 = Pin(26, Pin.OUT)
led_2 = Pin(25, Pin.OUT)
led_3 = Pin(33, Pin.OUT)
led_4 = Pin(32, Pin.OUT)
led_flash = Pin(5, Pin.OUT)
def binarycount(x):
binary='{0:05b}'.format(x)
led_0.value(int(binary[4]))
led_1.value(int(binary[3]))
led_2.value(int(binary[2]))
led_3.value(int(binary[1]))
led_4.value(int(binary[0]))
def map(x, in_min, in_max, out_min, out_max):
return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
def handler_0(event):
led_flash.value(not led_flash.value())
# Configure the ADC
adc = ADC(Pin(2))
adc.atten(ADC.ATTN_11DB) # sets for a maximum input voltage of 3.6 volts
adc.width(ADC.WIDTH_9BIT) # sets the bit width to 12 bits
# Enter while loop
tim0 = Timer(0)
tim0.init(period=1500, mode=Timer.PERIODIC, callback=handler_0)
while True:
adc_value = adc.read() # Read the voltage on SIG pin of potentiometer
volts = (3.3*adc_value)/511 # Convert raw adc value to actual voltage value
#Print out the actual voltage value and raw adc value
val = map(adc_value, 0, 511, 0, 31)
binarycount(val)
input_voltage = "The input voltage is {:0.1f} volts and raw value is {:3d}"
print(input_voltage .format(volts,adc_value),end = "\r")
'''
from machine import Pin, ADC, Timer
from time import sleep
#configuring the ADC
adc = ADC(Pin(2))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_9BIT)
def handler_0(time0):
led_5.value(not led_5.value())
#creating variable that is Timer
time0 = Timer(0)
time0.init(period=150, mode=Timer.PERIODIC, callback=handler_0)
#defining pins Used
led_0 = Pin(27, Pin.OUT)
led_1 = Pin(26, Pin.OUT)
led_2 = Pin(25, Pin.OUT)
led_3 = Pin(33, Pin.OUT)
led_4 = Pin(32, Pin.OUT)
led_5 = Pin(5, Pin.OUT)
#defining the function
def binarycount(x):
binary='{0:05b}' .format(x)
sleep(0.5)
led_0.value(int(binary[4]))
led_1.value(int(binary[3]))
led_2.value(int(binary[2]))
led_3.value(int(binary[1]))
led_4.value(int(binary[0]))
#Enter While Loop
while True:
adc_value = adc.read()
volts = (3.3*adc_value)/511
binary_32 = int(31*(adc_value/511))
#print out the actual voltage and raw value to correct three decimal place
input_voltage = "The input voltage is {:0.1f} volts and raw value is {:2d} Note:"
print(input_voltage. format(volts,adc_value,binary_32),end = "\r")
binarycount(binary_32)
'''