Skip to content

6. Input & Output device

In this week I used IOTKIds micro controller AKA ESP 32 WROOM to program in Adriano input and output and mixed connection.

Visit Embeded Programing week for more info

Buzzer Set-up

AI-tool to find code, check +RUN in Arduino IDE

Video

Code

#define buzzerPin 8 // Connect the buzzer to digital pin 26

void setup() {
  pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as output
}

void loop() {
  tone(buzzerPin, 1000); // Generate a 1kHz tone
  delay(1000); // Wait for 1 second
  noTone(buzzerPin); // Stop the tone
  delay(1000); // Wait for 1 second
}

Random

Video

Code:

#define buzzerPin 26 // Connect the buzzer to digital pin 8

void setup() {
  pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as output
}

void loop() {
  tone(buzzerPin, 1000); // Generate a 1kHz tone
  delay(random(250,1000)); // Wait for 1 second
  noTone(buzzerPin); // Stop the tone
  delay(random(250,1000)); // Wait for 1 second
}

Arduino Project hub

Visit website

Choose desired theme song

Main Page

Video

Pink Panther Original code

//Pink panther original code
// -------------------------------------------------
// Copyright (c) 2022 HiBit <https://www.hibit.dev>
// -------------------------------------------------

#include "pitches.h"

#define BUZZER_PIN 9

int melody[] = {
  REST, REST, REST, NOTE_DS4, 
  NOTE_E4, REST, NOTE_FS4, NOTE_G4, REST, NOTE_DS4,
  NOTE_E4, NOTE_FS4,  NOTE_G4, NOTE_C5, NOTE_B4, NOTE_E4, NOTE_G4, NOTE_B4,   
  NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_E4, NOTE_D4, 
  NOTE_E4, REST, REST, NOTE_DS4,

  NOTE_E4, REST, NOTE_FS4, NOTE_G4, REST, NOTE_DS4,
  NOTE_E4, NOTE_FS4,  NOTE_G4, NOTE_C5, NOTE_B4, NOTE_G4, NOTE_B4, NOTE_E5,
  NOTE_DS5,   
  NOTE_D5, REST, REST, NOTE_DS4, 
  NOTE_E4, REST, NOTE_FS4, NOTE_G4, REST, NOTE_DS4,
  NOTE_E4, NOTE_FS4,  NOTE_G4, NOTE_C5, NOTE_B4, NOTE_E4, NOTE_G4, NOTE_B4,   

  NOTE_AS4, NOTE_A4, NOTE_G4, NOTE_E4, NOTE_D4, 
  NOTE_E4, REST,
  REST, NOTE_E5, NOTE_D5, NOTE_B4, NOTE_A4, NOTE_G4, NOTE_E4,
  NOTE_AS4, NOTE_A4, NOTE_AS4, NOTE_A4, NOTE_AS4, NOTE_A4, NOTE_AS4, NOTE_A4,   
  NOTE_G4, NOTE_E4, NOTE_D4, NOTE_E4, NOTE_E4, NOTE_E4
};

int durations[] = {
  2, 4, 8, 8, 
  4, 8, 8, 4, 8, 8,
  8, 8,  8, 8, 8, 8, 8, 8,   
  2, 16, 16, 16, 16, 
  2, 4, 8, 4,

  4, 8, 8, 4, 8, 8,
  8, 8,  8, 8, 8, 8, 8, 8,
  1,   
  2, 4, 8, 8, 
  4, 8, 8, 4, 8, 8,
  8, 8,  8, 8, 8, 8, 8, 8,   

  2, 16, 16, 16, 16, 
  4, 4,
  4, 8, 8, 8, 8, 8, 8,
  16, 8, 16, 8, 16, 8, 16, 8,   
  16, 16, 16, 16, 16, 2
};

void setup()
{
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop()
{
  int size = sizeof(durations) / sizeof(int);

  for (int note = 0; note < size; note++) {
    //to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int duration = 1000 / durations[note];
    tone(BUZZER_PIN, melody[note], duration);

    //to distinguish the notes, set a minimum time between them.
    //the note's duration + 30% seems to work well:
    int pauseBetweenNotes = duration * 1.30;
    delay(pauseBetweenNotes);

    //stop the tone playing:
    noTone(BUZZER_PIN);
  }
}

Pink panther edited

Video

Pink panther code without pitches.h

// Define pin 10 for buzzer, you can use any other digital pins (Pin 0-13)
const int buzzer = 26;

// Change to 0.5 for a slower version of the song, 1.25 for a faster version
const float songSpeed = 1;

// Defining frequency of each music note
#define NOTE_DS 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_D5 587
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_G5 784
#define NOTE_A5 880
#define NOTE_B5 988

// Music notes of the song, 0 is a rest/pulse
int notes[] = {
    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
    NOTE_A4, NOTE_G4, NOTE_A4, 0,

    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
    NOTE_A4, NOTE_G4, NOTE_A4, 0,

    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
    NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
    NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,

    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_D5, NOTE_E5, NOTE_A4, 0,
    NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
    NOTE_C5, NOTE_A4, NOTE_B4, 0,

    NOTE_A4, NOTE_A4,
    //Repeat of first part
    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
    NOTE_A4, NOTE_G4, NOTE_A4, 0,

    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
    NOTE_A4, NOTE_G4, NOTE_A4, 0,

    NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
    NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
    NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
    NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,

    NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
    NOTE_D5, NOTE_E5, NOTE_A4, 0,
    NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
    NOTE_C5, NOTE_A4, NOTE_B4, 0,
    //End of Repeat

    NOTE_E5, 0, 0, NOTE_F5, 0, 0,
    NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
    NOTE_D5, 0, 0, NOTE_C5, 0, 0,
    NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4,

    NOTE_E5, 0, 0, NOTE_F5, 0, 0,
    NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
    NOTE_D5, 0, 0, NOTE_C5, 0, 0,
    NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4};

// Durations (in ms) of each music note of the song
// Quarter Note is 250 ms when songSpeed = 1.0
int durations[] = {
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 125,

    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 125,

    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 125, 250, 125,

    125, 125, 250, 125, 125,
    250, 125, 250, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 375,

    250, 125,
    //Rpeat of First Part
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 125,

    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 125,

    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 250, 125, 125,
    125, 125, 125, 250, 125,

    125, 125, 250, 125, 125,
    250, 125, 250, 125,
    125, 125, 250, 125, 125,
    125, 125, 375, 375,
    //End of Repeat

    250, 125, 375, 250, 125, 375,
    125, 125, 125, 125, 125, 125, 125, 125, 375,
    250, 125, 375, 250, 125, 375,
    125, 125, 125, 125, 125, 500,

    250, 125, 375, 250, 125, 375,
    125, 125, 125, 125, 125, 125, 125, 125, 375,
    250, 125, 375, 250, 125, 375,
    125, 125, 125, 125, 125, 500};

void setup()
{
  const int totalNotes = sizeof(notes) / sizeof(int);
  // Loop through each note
  for (int i = 0; i < totalNotes; i++)
  {
    const int currentNote = notes[i];
    float wait = durations[i] / songSpeed;
    // Play tone if currentNote is not 0 frequency, otherwise pause (noTone)
    if (currentNote != 0)
    {
      tone(buzzer, notes[i], wait); // tone(pin, frequency, duration)
    }
    else
    {
      noTone(buzzer);
    }
    // delay is used to wait for tone to finish playing before moving to next loop
    delay(wait);
  }
}

void loop()
{
  // Nothing in loop. Music only plays once.
  // You can click reset on Arduino to replay the song.
}

RGB LED – color-shifting

Video

Set-up

Code

#define RED_PIN 12
#define GREEN_PIN 13
#define BLUE_PIN 14

void setup() {
  // Set the RGB pins as output
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop() {
  // Red
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);
  delay(1000);

  // Green
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, LOW);
  delay(1000);

  // Blue
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);

  // Yellow
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, LOW);
  delay(1000);

  // Cyan
  digitalWrite(RED_PIN, LOW);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);

  // Magenta
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);

  // White
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, HIGH);
  digitalWrite(BLUE_PIN, HIGH);
  delay(1000);
}

RGB LED – fade-green-red-blue

Video

Code

#define RED_PIN 12
#define GREEN_PIN 13
#define BLUE_PIN 14

void setup() {
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
}

void loop() {
  // Fade in red
  for (int i = 0; i <= 255; i++) {
    analogWrite(RED_PIN, i);
    delay(10);
  }

  // Fade out red, fade in green
  for (int i = 255; i >= 0; i--) {
    analogWrite(RED_PIN, i);
    analogWrite(GREEN_PIN, 255 - i);
    delay(10);
  }

  // Fade out green, fade in blue
  for (int i = 255; i >= 0; i--) {
    analogWrite(GREEN_PIN, i);
    analogWrite(BLUE_PIN, 255 - i);
    delay(10);
  }

  // Fade out blue, fade in red
  for (int i = 255; i >= 0; i--) {
    analogWrite(BLUE_PIN, i);
    analogWrite(RED_PIN, 255 - i);
    delay(10);
  }
}

Input

Film pressure sensor (analog) (green color)

Results

Set up

Code

#include <Arduino.h>

const int sensorPin = 4; // Analog pin connected to the pressure sensor

void setup() {
  Serial.begin(9600); // Initialize serial communication
}

void loop() {
  int sensorValue = analogRead(sensorPin); // Read the analog value from the sensor

  // Convert the sensor value to voltage (optional)
  float voltage = sensorValue * (5.0 / 1023.0); // Assuming 5V supply

  // Map the sensor value to a pressure range (adjust these values based on your sensor's specifications)
  float pressure = map(sensorValue, 0, 1023, 0, 100); // Example: Map to 0-100 units of pressure

  Serial.print("Sensor Value: ");
  Serial.print(sensorValue);
  Serial.print(" | Voltage: ");
  Serial.print(voltage);
  Serial.print(" | Pressure: ");
  Serial.print(pressure);
  Serial.println(" units");

  delay(100); // Delay to slow down the readings
}

Hall Magnetic Sensor (analog)

Video

Set-up

Code

#include <Arduino.h>

const int hallSensorPin = 4; // Analog pin connected to the Hall sensor

void setup() {
  Serial.begin(9600); // Initialize serial communication
}

void loop() {
  int sensorValue = analogRead(hallSensorPin); // Read sensor value

  // Map sensor value to a more readable range (optional)
  int mappedValue = map(sensorValue, 0, 1023, 0, 100); 

  Serial.print("Hall Sensor Value: ");
  Serial.println(mappedValue);

  delay(500); // Delay between readings
}

Potentiometer

Set-up (Analog)

Video

Code

#include <Arduino.h>

const int potPin = 39; // Analog pin connected to the potentiometer

void setup() {
  Serial.begin(9600); // Initialize serial communication
}

void loop() {
  int sensorValue = analogRead(potPin); // Read the value from the potentiometer

  // Map the sensor value to a 0-100 range (optional)
  int mappedValue = map(sensorValue, 0, 1023, 0, 100); 

  Serial.print("Potentiometer Value: ");
  Serial.println(mappedValue);

  delay(100); // Delay between readings
}

Mixed (1 input/ 1 output)

Buzzer/ Sensor (horn)

Video

Set-up

Code

#include <Arduino.h>

const int sensorPin = 4; // Analog pin connected to the pressure sensor
const int buzzerPin = 27; // Digital pin connected to the buzzer

void setup() {
  Serial.begin(9600); // Initialize serial communication
  pinMode(buzzerPin, OUTPUT); // Set buzzer pin as output
}

void loop() {
  int sensorValue = analogRead(sensorPin); // Read sensor value

  // Map sensor value to a pressure range (adjust as needed)
  float pressure = map(sensorValue, 0, 1023, 0, 100); 

  // Check if pressure exceeds a threshold (adjust threshold as needed)
  if (pressure < 300) { // Example: Trigger buzzer above 50 pressure units
    tone(buzzerPin, 440); // Generate a 440Hz tone (A4)
  } else {
    noTone(buzzerPin); // Stop the buzzer
  }

  // Serial output (optional)
  Serial.print("Pressure: ");
  Serial.print(pressure);
  Serial.println(" units");

  delay(100); // Delay between readings
}

RGB LED controlled

Video

Set-up

Code

#include <Arduino.h>

const int potPin = 39; // Analog pin connected to the potentiometer
const int redPin = 12; 
const int greenPin = 13;
const int bluePin = 14;

void setup() {
  // Set the RGB pins as output
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  Serial.begin(9600); // Initialize serial communication
}

void loop() {
  int sensorValue = analogRead(potPin); 

  // Limit sensorValue to a maximum of 4095 
  if (sensorValue > 4095) {
    sensorValue = 4095; 
  }

  // Map the sensor value to a 0-255 range for PWM
  int red, green, blue; 

  // Divide the potentiometer range into more segments for smoother transitions
  if (sensorValue < 409) { 
    // Fade in red
    red = map(sensorValue, 0, 409, 0, 255); 
    green = 0; 
    blue = 0; 
  } else if (sensorValue < 818) { 
    // Fade from red to orange
    red = 255; 
    green = map(sensorValue, 409, 818, 0, 255); 
    blue = 0; 
  } else if (sensorValue < 1227) { 
    // Fade from orange to yellow
    red = 255; 
    green = 255; 
    blue = 0; 
  } else if (sensorValue < 1636) { 
    // Fade from yellow to green
    red = 255 - map(sensorValue, 1227, 1636, 255, 0); 
    green = 255; 
    blue = 0; 
  } else if (sensorValue < 2045) { 
    // Fade from green to cyan
    red = 0; 
    green = 255; 
    blue = map(sensorValue, 1636, 2045, 0, 255); 
  } else if (sensorValue < 2454) { 
    // Fade from cyan to blue
    red = 0; 
    green = 255 - map(sensorValue, 2045, 2454, 255, 0); 
    blue = 255; 
  } else if (sensorValue < 2863) { 
    // Fade from blue to purple
    red = map(sensorValue, 2454, 2863, 0, 255); 
    green = 0; 
    blue = 255; 
  } else if (sensorValue < 3272) { 
    // Fade from purple to magenta
    red = 255; 
    green = 0; 
    blue = 255 - map(sensorValue, 2863, 3272, 255, 0); 
  } else if (sensorValue < 3681) { 
    // Fade from magenta to red
    red = 255; 
    green = map(sensorValue, 3272, 3681, 0, 255); 
    blue = 0; 
  } else { 
    // Fade to off
    red = 255 - map(sensorValue, 3681, 4090, 255, 0); 
    green = 0; 
    blue = 0; 
  }

  // Set the brightness of the LEDs
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);

  // Print sensor value and mapped colors to serial monitor
  Serial.print("Sensor Value: ");
  Serial.print(sensorValue);
  Serial.print(" | Red: ");
  Serial.print(red);
  Serial.print(" | Green: ");
  Serial.print(green);
  Serial.print(" | Blue: ");
  Serial.println(blue);

  delay(10); // Delay between readings
}

Turbine-potentiometer

Video

Set-up

Code

#include <Arduino.h>

// Define the pin connected to the fan's PWM input
const int fanPin = 26; 

void setup() {
  // Set the fan pin as an output for PWM control
  pinMode(fanPin, OUTPUT);
  Serial.begin(9600); // Initialize serial communication
}

void loop() {
  // Example: Control fan speed based on a potentiometer reading
  // Replace with your actual sensor input or desired speed control logic

  // Read potentiometer value (replace with your sensor reading)
  int sensorValue = analogRead(39); 

  // Map sensor value to PWM duty cycle (0-255)
  int fanSpeed = map(sensorValue, 0, 4095, 0, 255); 

  // Set the fan speed using PWM
  analogWrite(fanPin, fanSpeed); 

  // Print the fan speed to the serial monitor
  Serial.print("Fan Speed: ");
  Serial.println(fanSpeed);
  Serial.print("Sensor Value: ");
  Serial.print(sensorValue);
  delay(100); // Delay for smoother speed control
}

Turbine directional rotation

Video

Set-up

Code

#include <Arduino.h>

// Define the pins connected to the H-bridge
const int motor1Pin1 = 12;
const int motor1Pin2 = 13;
const int motor1EnablePin = 14;

// Define the potentiometer pin
const int potPin = 39;

void setup() {
  // Set the motor control pins as output
  pinMode(motor1Pin1, OUTPUT);
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor1EnablePin, OUTPUT);

  // Initialize serial communication for debugging
  Serial.begin(9600);
}

void loop() {
  // Read potentiometer value
  int sensorValue = analogRead(potPin);

  // Map sensor value to a speed range (0-255)
  int motorSpeed = map(sensorValue, 0, 4095, 0, 255);

  // Control the motor direction based on the sensor value
  if (sensorValue < 2050) {
    // Clockwise rotation
    digitalWrite(motor1Pin1, LOW);
    digitalWrite(motor1Pin2, HIGH);
  } else {
    // Anticlockwise rotation
    digitalWrite(motor1Pin1, HIGH);
    digitalWrite(motor1Pin2, LOW);
  }

  // Set the motor speed using PWM
  analogWrite(motor1EnablePin, motorSpeed);

  // Print the sensor value and motor speed to the serial monitor
  Serial.print("Sensor Value: ");
  Serial.print(sensorValue);
  Serial.print(", Motor Speed: ");
  Serial.println(motorSpeed);

  delay(100); // Delay for smoother control
}

Last update: December 19, 2024