Skip to content

7. Input & Output device

This week I worked on defining my final project idea and started to getting used to the documentation process.

This week, the goal was to work with an I/O (input/output) device and program it to perform a specific task, such as turning on a fan and controlling its speed. Additionally, you continued progressing on the gas lake sensor project, which involved setting up various connections, including a screen and a buzzer.
For more information about the project, visit Faisal Fathi

Operating the Fan: We reiterate that the microcontroller board will be connected using the same previous steps. Afterward, we will proceed to program the fan.

When the Run code:

Following the execution of the code:

// the setup function runs once when you press reset or power the board
import machine
import time

# Define the pin for the buzzer
buzzer_pin = machine.Pin(26, machine.Pin.OUT)
buzzer_pwm = machine.PWM(buzzer_pin)

def play_tone(frequency, duration):
    buzzer_pwm.freq(frequency)
    buzzer_pwm.duty(512)  # Set duty cycle to 50% for square wave
    time.sleep(duration)
    buzzer_pwm.duty(0)  # Turn off buzzer

# Example sound effects inspired by Mario game sounds
def play_mario_jump():
    # Simulate a jump sound
    tones = [
        (523, 0.5),  # C5 note, short duration
        (659, 0.5),  # E5 note, short duration
        (784, 0.1)    # G5 note, longer duration
    ]
    for frequency, duration in tones:
        play_tone(frequency, duration)
        time.sleep(0.5)  # Short pause between notes

def play_mario_coin():
    # Simulate a coin sound
    tones = [
        (1046, 0.5),  # C6 note, short duration
        (1318, 0.5)   # E6 note, short duration
    ]
    for frequency, duration in tones:
        play_tone(frequency, duration)
        time.sleep(0.5)  # Short pause between notes

def play_mario_powerup():
    # Simulate a power-up sound
    tones = [
        (659, 0.1),  # E5 note
        (784, 0.1),  # G5 note
        (988, 0.1),  # B5 note
        (1318, 0.1)  # E6 note
    ]
    for frequency, duration in tones:
        play_tone(frequency, duration)
        time.sleep(0.5)  # Short pause between notes

# Play the sound effects
play_mario_jump()
time.sleep(1)  # Wait 1 second between sounds
play_mario_coin()
time.sleep(1)
play_mario_powerup()

Turn 4 LED in one pin: We reiterate that the microcontroller board will be connected using the same steps as in previous steps. Afterward, we will proceed to the program.

When the Run code:

Following the execution of the code:

import machine
import neopixel
import time

# Define the pin number and number of LEDs
LED_PIN = 18
NUM_LEDS = 4  # Change this to the number of LEDs in your strip

# Initialize the NeoPixel object
np = neopixel.NeoPixel(machine.Pin(LED_PIN), NUM_LEDS)

# Function to set the color of all LEDs
def set_color(r, g, b):
    for i in range(NUM_LEDS):
        np[i] = (r, g, b)
    np.write()

# Function to turn off all LEDs
def turn_off():
    set_color(0, 0, 0)

# Example: Cycle through different colors
def cycle_colors():
    colors = [
        (169, 44, 33),    # Red
        (0, 255, 90),    # Green
        (0, 0, 255),    # Blue
        (255, 255, 0),  # Yellow
        (0, 255, 255),  # Cyan
        (255, 0, 255),  # Magenta
        (255, 255, 255) # White
    ]

    for color in colors:
        set_color(*color)
        time.sleep(3)  # Wait  second between color changes
        turn_off()
        time.sleep(1)  # Short pause between colors

# Run the color cycle
cycle_colors()

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
}

Last update: September 11, 2024