5. Input & Output device¶
This week I worked on microcontrollers and tried different inputs and outputs.
Traffic Light¶
Output¶
RGB LED light which indicates for a traffic light and it has 3 pins for red, green and blue
Code¶
from machine import Pin
import time
RLED =Pin(12,Pin.OUT)
YLED =Pin(13,Pin.OUT)
GLED =Pin(14,Pin.OUT)
Fan=Pin(2,Pin.OUT)
button = Pin(0, Pin.IN, Pin.PULL_UP)
while True:
RLED.value(1)
time.sleep(2)
YLED.value(1)
time.sleep(2)
RLED.value(0)
YLED.value(0)
GLED.value(1)
time.sleep(2)
GLED.value(0)
YLED.value(1)
time.sleep(2)
YLED.value(0)
Result¶
Fan¶
Input¶
A rotary potentiometer is a type of adjustable resistor that uses a rotating shaft to change its resistance. It consists of a circular resistive element, a wiper that moves along this element, and three terminals: two connected to the ends of the resistive element and one to the wiper. By turning the shaft, the position of the wiper changes, varying the resistance between the terminals and thus adjusting the output voltage. Commonly used in applications like volume controls, light dimmers, and tuning devices, rotary potentiometers are valued for their simplicity and durability, though they can experience mechanical wear over time.
Output¶
A fan motor is an electric motor that powers a fan to create airflow, used in various applications such as household appliances, HVAC systems, and electronic cooling devices. It consists of a stator, rotor, and fan blades, with the stator generating a magnetic field that turns the rotor and attached blades to move air. Fan motors come in different types, including AC motors for household fans, DC motors for devices like computer fans, and brushless DC motors for efficiency and quiet operation. They are essential for cooling, ventilation, and maintaining optimal temperatures in a wide range of environments.
Code¶
from machine import Pin, ADC, PWM
import time
# Set up the fan control pin with PWM for speed control
fan = PWM(Pin(13), freq=1000)
# Set up the potentiometer (ADC)
pot = ADC(Pin(39)) # Adjust the pin number based on your board
pot.atten(ADC.ATTN_11DB) # Set the attenuation for full range (0-3.3V)
pot.width(ADC.WIDTH_10BIT) # Set the width to 10 bits (0-1023)
while True:
# Read the potentiometer value
pot_value = pot.read()
# Map the potentiometer value to the PWM duty cycle
fan_duty = int((pot_value / 1023) * 1023) # Scale the value to 0-1023
# Set the fan speed
fan.duty(fan_duty)
# Small delay to avoid excessive CPU usage
time.sleep(0.01)
Result¶
Door Bell¶
Input¶
A pushbutton is a mechanical switch that is activated by pressing it to make or break an electrical circuit, allowing users to control devices and functions. It typically consists of a button, housing, contacts, and a spring that returns the button to its original position. Pushbuttons can be normally open or normally closed and come in momentary or maintained types. They are widely used in consumer electronics, industrial controls, automotive applications, and home appliances due to their simplicity, reliability, and versatility, although they can experience mechanical wear over time.
Output¶
A buzzer is an electronic device that produces an audible sound or tone when activated, typically used for signaling or alert purposes. It operates by converting electrical energy into sound through mechanical, electromagnetic, or piezoelectric means. Commonly found in alarms, timers, and user interfaces, buzzers are essential for providing auditory feedback or warnings in various applications, including household appliances, automotive systems, and electronic gadgets. Buzzers are valued for their simplicity, reliability, and effectiveness in drawing attention.
Code¶
from machine import Pin, PWM
import time
# Set up the buzzer pin with PWM for sound control
buzzer = PWM(Pin(2), freq=1000)
# Set up the button pin with a pull-up resistor
button = Pin(0, Pin.IN, Pin.PULL_UP)
# Debounce time in milliseconds
debounce_time = 200
last_button_press = 0
# Function to play a tone
def play_tone(frequency, duration):
buzzer.freq(frequency)
buzzer.duty(512) # 50% duty cycle
time.sleep(duration)
buzzer.duty(0) # Turn off the buzzer
time.sleep(0.05) # Short delay between notes
# Function to play the Pink Panther theme
def play_pink_panther_theme():
notes = [
(392, 0.4), # G4
(494, 0.4), # B4
(523, 0.4), # C5
(494, 0.4), # B4
(392, 0.4), # G4
(523, 0.4), # C5
(494, 0.4), # B4
(392, 0.4), # G4
(349, 0.4), # F4
(294, 0.4), # D4
(349, 0.4), # F4
(392, 0.4), # G4
(294, 0.4), # D4
(349, 0.4), # F4
(392, 0.4), # G4
]
for note in notes:
play_tone(note[0], note[1])
while True:
# Read the button state
button_state = button.value()
# Check if the button is pressed (active low)
if button_state == 0:
# Check for debounce
if (time.ticks_ms() - last_button_press) > debounce_time:
# Play the Pink Panther theme
play_pink_panther_theme()
# Update the last button press time
last_button_press = time.ticks_ms()
# Small delay to avoid excessive CPU usage
time.sleep(0.01)
Result¶
Research¶
“Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.”
“Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.”
Useful links¶
Code Example¶
Use the three backticks to separate code.
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}
// the loop function runs over and over again forever
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Gallery¶
files¶
click to download my car model
Video¶
From Vimeo¶
Sound Waves from George Gally (Radarboy) on Vimeo.