Basic Arduino Codes by ChatGpt
Basic Arduino Codes by ChatGpt
9-10,2024
### **1. Blinking LED**
**Materials:**
- 1 x Arduino Uno
- 1 x LED
- 1 x 220Ω resistor
- Breadboard
- Jumper wires
**Code:**
void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}
void loop() {
digitalWrite(ledPin, HIGH); // Turn the LED on
delay(1000); // Wait for 1 second
digitalWrite(ledPin, LOW); // Turn the LED off
delay(1000); // Wait for 1 second
}
### **2. Traffic Light Controller**
**Materials:**
- 1 x Arduino Uno
- 1 x Red LED
- 1 x Yellow LED
Teresa National High School Robotics Training Sept.9-10,2024
- 1 x Green LED
- 3 x 220Ω resistors
- Breadboard
- Jumper wires
**Code:**
**Materials:**
- 1 x Arduino Uno
- Breadboard
- Jumper wires
**Code:**
void setup() {
Serial.begin(9600); // Start serial communication
}
void loop() {
int tempReading = analogRead(tempPin); // Read sensor value
float voltage = tempReading * (5.0 / 1023.0); // Convert reading to voltage
float temperature = voltage * 100; // Convert voltage to temperature (Celsius)
Serial.println(temperature); // Print temperature to serial monitor
delay(1000); // Update every 1 second
}
**Materials:**
- 1 x Arduino Uno
Teresa National High School Robotics Training Sept.9-10,2024
- 1 x HC-SR04 Ultrasonic Sensor
- Breadboard
- Jumper wires
**Code:**
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
long duration;
int distance;
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000);
}
**Materials:**
- 1 x Arduino Uno
- 1 x Microphone sensor
- 1 x LED
- 1 x 220Ω resistor
- Breadboard
- Jumper wires
**Code:**
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Teresa National High School Robotics Training Sept.9-10,2024
}
void loop() {
int soundLevel = analogRead(micPin); // Read microphone input
Serial.println(soundLevel);
Each project includes simple components and foundational Arduino concepts. With explanations of code and
components, these projects will make a great start for beginners!