Arduino Projects: Traffic Light, Button, Motor & LED Control

Classified in Technology

Written at on English with a size of 2.54 KB.

Traffic Light with Arduino

This code simulates a traffic light using an Arduino.


int led_rojo = 3;
int led_verde = 4;
int led_amarillo = 5;

void setup() {
  pinMode(led_rojo, OUTPUT);  // PIN 3 as output
  pinMode(led_verde, OUTPUT); // PIN 4 as output
  pinMode(led_amarillo, OUTPUT); // PIN 5 as output
}

void loop() {
  digitalWrite(led_verde, HIGH); // Turn on green LED
  delay(2000);
  digitalWrite(led_verde, LOW);  // Turn off green LED
  delay(500);
  digitalWrite(led_amarillo, HIGH); // Turn on yellow LED
  delay(2000);
  digitalWrite(led_amarillo, LOW);  // Turn off yellow LED
  delay(500);
  digitalWrite(led_rojo, HIGH);   // Turn on red LED
  delay(2000);
  digitalWrite(led_rojo, LOW);    // Turn off red LED
  delay(500);
}

Button Control with Arduino

This code controls an LED using two buttons.


int switch1 = 3;
int switch2 = 4;
int ledpin = 13;

void setup() {
  pinMode(3, INPUT);
  pinMode(4, INPUT);
  pinMode(13, OUTPUT);
}

void loop() {
  if (digitalRead(3) == HIGH) {
    digitalWrite(13, HIGH);
  }
  if (digitalRead(4) == HIGH) {
    digitalWrite(13, LOW);
  }
}

Motor Speed Control with Arduino

This code controls a motor's speed using a potentiometer.


const int analogInPin = A5;  // Analog input pin for potentiometer
const int analogOutPin = 9; // Analog output pin for LED (PWM)
int sensorValue = 0;         // Value read from the potentiometer
int outputValue = 0;         // Value output to the PWM (analog out)

void setup() {
  Serial.begin(9600);
}

void loop() {
  sensorValue = analogRead(analogInPin);
  outputValue = map(sensorValue, 0, 1023, 0, 255);
  analogWrite(analogOutPin, outputValue);

  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\t output = ");
  Serial.println(outputValue);

  delay(2);
}

LED Blinking with Arduino

This code makes an LED blink on and off.


void setup() {
  pinMode(13, OUTPUT); // Initialize pin 13 as output for the LED
}

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

Microcontroller System Units

The main units of a microcontroller system are:

  1. Input/Output Unit
  2. RAM Memory
  3. ROM Memory
  4. Arithmetic Logic Unit (ALU)
  5. Control Unit

Simple LED Circuit

A basic circuit using an LED includes the LED and a resistor.

Entradas relacionadas: