# Project#6 - Let's make a Light Meter # Connections: Add jumpers to the LED headers (Labelled G16BLU and G17RED) to the left of the coin battery. # Also add jumpers to the GP15 S1 header and GP14 S2 headers to the right of the coin battery socket. # The DIP swtiche channels are tied to GPIOs 18-thrrough 21 # Add a jumper to the BUZ/G22 header to connect GP22 to the buzzer control circuit. # Add a jumper to the MIC/AD1 header to connect the AD1/GP27 pin to the microphone output # Add a jumper to the header just behind the light sensor (Right between the microphone and the light sensor) import machine # Think of "import" as importing a library. This library allows for us to use GPIOs import utime # This "library" allows for us to tell time if connected to the internet, and use delays like 'utime.sleep(1)' #The below declarations are new since project#5 LightSensor = machine.ADC(26) # Setup GPIO26 to act as the ADC input for the light sensor. # Buzzer = machine.Pin(22, machine.Pin.OUT) #Setup the Buzzer, which is connected to GPIO 22. Declare it as an output. Microphone = machine.ADC(27) # Setup GPIO27 to act as the ADC input for the microphone. This is the AD1 line Button = machine.Pin(15, machine.Pin.IN) #Setup the SEL1 button pin to GPIO 15. Set it as an input and name it "Button" Button2 = machine.Pin(14, machine.Pin.IN) #Setup the SEL2 button pin to GPIO 14. Set it as an input and name it "Button2" DIPA = machine.Pin(21, machine.Pin.IN) #Setup DIP Switch Channel-A as an output DIPB = machine.Pin(20, machine.Pin.IN) #Setup DIP Switch Channel-B as an output DIPC = machine.Pin(19, machine.Pin.IN) #Setup DIP Switch Channel-C as an output DIPD = machine.Pin(18, machine.Pin.IN) #Setup DIP Switch Channel-D as an output LEDRED = machine.Pin(16, machine.Pin.OUT) # Set up GPIO16 as an output, and name it LEDRED LEDBLU = machine.Pin(17, machine.Pin.OUT) # Do the same for GPIO17 LEDRED.value(0) # Turn the red LED off. If you set it to (1), then you'd be turning it on LEDBLU.value(0) # Turn the blue LED off. If you set it to (1), then you'd be turning it on state = 0 # state is a variable/storage register. Set it to a value of 0. hold = 0 # hold is another variable that we'll use to store data in conversion = 3.3/(65535) # Conversion helps us to turn ADC readings (Numeral values) into a voltage representation. # 3.3v is the full scale voltage. 0-65565 is the full 16-bit scale value used for ADC conversions. 0000000000000000-1111111111111111 def Samplelight(): # This function samples the light sensor every second, and tells you whether there is light or darkness in the room utime.sleep(1) # Sleep for one full second print("Sampling Light...") # Print this to the shell #print("Light Sensor Value",LightSensor.read_u16()) hold = LightSensor.read_u16() # Sample the light sensor ADC value and store it into 'hold' hold = hold*conversion # Convert the ADC value into a voltage representation. This is talked about more in the previous project. print(hold,"v") # I love this. You can print more than one thing at a time, and it doesn't need to be text. # The above line says to print the converted value stored in hold, followed by the text "v". if hold > 1.5: print("The Room is Dark") else: print("The Room is Light") # The following is the main loop. It will loop calling the Samplelight() function over and over without end. while True: Samplelight()