Skip to content

6. week6

Output

We use on spark Arduino

(A-BUZZER)

The A-Buzzer or Pine is a simple electronic component that makes a sound when connected to an electric voltage. It is widely used in many electronic devices to issue sound alerts or signals.

Code:

// Pin definition
const int buzzerPin = 18; // Connect your buzzer to pin 18

// Define melody notes (frequency in Hz) and durations (in milliseconds)
int melody[] = {
  330, 400,  // E4
  294, 400,  // D4
  262, 400,  // C4
  294, 400,  // D4
  330, 400,  // E4
  330, 400,  // E4
  330, 400,  // E4
  294, 400,  // D4
  294, 400,  // D4
  330, 400,  // E4
  294, 400,  // D4
  262, 400   // C4
};

// Function to play a note
void playTone(int frequency, int duration) {
  if (frequency > 0) {
    tone(buzzerPin, frequency); // Play frequency
  }
  delay(duration); // Wait for the duration
  noTone(buzzerPin); // Stop sound
}

void setup() {
  // Nothing to set up
}

void loop() {
  for (int i = 0; i < sizeof(melody) / sizeof(melody[0]); i += 2) {
    playTone(melody[i], melody[i + 1]); // Play note and duration
  }
  delay(1000); // Wait before repeating
}

Experiment video

RGB LED

They are light bulbs consisting of three basic colors: red, green, and blue, hence the name RGB. By mixing these three colors in different proportions, we can get millions of different colors.

Code:

// Pin definitions
const int redPin = 17;    // Connect the red pin to pin 17
const int greenPin = 18; // Connect the green pin to pin 18
const int bluePin = 19;  // Connect the blue pin to pin 19

void setup() {
  // Set RGB pins as outputs
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
}

void loop() {
  // Cycle through colors
  setColor(255, 0, 0);   // Red
  delay(1000);
  setColor(0, 255, 0);   // Green
  delay(1000);
  setColor(0, 0, 255);   // Blue
  delay(1000);
  setColor(255, 255, 0); // Yellow
  delay(1000);
  setColor(0, 255, 255); // Cyan
  delay(1000);
  setColor(255, 0, 255); // Magenta
  delay(1000);
  setColor(255, 255, 255); // White
  delay(1000);
  setColor(0, 0, 0);     // Off
  delay(1000);
}

// Function to set the color of the RGB LED
void setColor(int red, int green, int blue) {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}

Experiment video

Output-Input

We use Film pressure and LED LED first running LED Code:

const int ledPin = 18; // GPIO pin connected to the LED

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

  // Set the LED pin as an output
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // Turn the LED on
  digitalWrite(ledPin, HIGH);
  delay(1000); // Wait for 1 second

  // Turn the LED off
  digitalWrite(ledPin, LOW);
  delay(1000); // Wait for 1 second
}

Then we put the LED and film pressure code:

A film pressure is an electronic device designed to measure the amount of force or pressure acting on it and convert it into an electrical signal that can be read and processed. Imagine that it is the mouthpiece of devices, as it translates physical forces into a language that electronic devices understand.

Code:

const int ledPin = 18; // GPIO pin connected to the LED
const int sensorPin = 34; // Analog pin connected to the pressure sensor

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

  // Set the LED pin as an output
  pinMode(ledPin, OUTPUT);
}

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

  // Map the sensor value to a pressure range (adjust as needed)
  int pressure = map(sensorValue, 0, 4095, 0, 100); // Adjust the range based on your sensor

  // Control the LED based on pressure
  if (pressure > 50) { // Adjust the threshold as needed
    digitalWrite(ledPin, HIGH); // Turn on the LED
  } else {
    digitalWrite(ledPin, LOW); // Turn off the LED
  }

  // Print the sensor value and pressure to the serial monitor
  Serial.print("Sensor Value: ");
  Serial.println(sensorValue);
  Serial.print("Pressure: ");
  Serial.print(pressure);
  Serial.println(" units");

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

Experiment video

LED with potentiometer experiment:

A potentiometer is a three-terminal electrical resistor, the resistive value of which can be changed by moving a moving pointer along a base resistance. In other words, it is a variable resistance whose value we can control manually.

Code:

const int ledPin = 18; // GPIO pin connected to the LED
const int potPin = 34; // Analog pin connected to the potentiometer

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

  // Set the LED pin as an output
  pinMode(ledPin, OUTPUT);
}

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

  // Map the potentiometer value to a brightness level (0-255)
  int brightness = map(potValue, 0, 1023, 0, 255); 

  // Control the LED brightness using PWM
  analogWrite(ledPin, brightness);

  // Print the potentiometer value and brightness to the serial monitor
  Serial.print("Potentiometer Value: ");
  Serial.println(potValue);
  Serial.print("Brightness: ");
  Serial.println(brightness);

  delay(10); // Short delay for smoother brightness control
}

Experiment video

Hall magnetic experiment:

Hall magnetic is an electronic device used to measure the strength and direction of the magnetic field. The principle of the work of this sensor depends on the phenomenon of Hall, a physical phenomenon that occurs when an electrical current flows through a place in a magnetic field.

Code:

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

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

  // Set the Hall sensor pin as an input
  pinMode(hallSensorPin, INPUT); 
}

void loop() {
  // Read the state of the Hall sensor
  int hallSensorState = analogRead(hallSensorPin);

  // Print the sensor state to the serial monitor
  Serial.print("Hall Sensor State: ");
  Serial.println(hallSensorState); 

  // You can use the sensor state to control other components
  // For example:
  if (hallSensorState == HIGH) {
    // Do something when the magnet is detected (e.g., turn on an LED)
    // ...
  } else {
    // Do something when the magnet is not detected (e.g., turn off an LED)
    // ...
  }

  delay(500); // Short delay for smoother readings
}

Experiment video


Last update: December 19, 2024