Link Ads

More search type here...

January 5, 2024

Running LED Knight Rider using Arduino

 Schematic


 The code:

int ledPins[] = {13, 12, 11, 10, 9, 8, 7, 6,5,4};
int numLEDs = sizeof(ledPins) / sizeof(ledPins[0]);
int delayTime = 80; // Waktu tunda dalam milidetik

void setup() {
  for (int i = 0; i < numLEDs; i++) {
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  for (int i = 0; i < numLEDs; i++) {
    digitalWrite(ledPins[i], HIGH);
    delay(delayTime);
    digitalWrite(ledPins[i], LOW);
  }
  for (int i = numLEDs - 1; i >= 0; i--) {
    digitalWrite(ledPins[i], HIGH);
    delay(delayTime);
    digitalWrite(ledPins[i], LOW);
  }
}


Use Arduino as Bateray AA Checker

 Schematic


 
Maximum input voltage measurement is 5 volt as maximum internal ADC in IC Atmega328. If want to expand measurement can use voltage divider circuit.  

The code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);  // Alamat I2C untuk LCD 16x2

const int sensorPin = A0;  // Pin sensor tegangan
const int redLedPin = 8;   // Pin LED merah
const int yellowLedPin = 9;  // Pin LED kuning
const int greenLedPin = 10;  // Pin LED hijau

void setup() {
  lcd.begin(16, 2);  // Inisialisasi LCD
  lcd.backlight();   // Aktifkan backlight LCD
  pinMode(redLedPin, OUTPUT);
  pinMode(yellowLedPin, OUTPUT);
  pinMode(greenLedPin, OUTPUT);
}

void loop() {
  // Baca tegangan dari sensor
  int sensorValue = analogRead(sensorPin);

  // Konversi nilai sensor menjadi tegangan (0-5V)
  float voltage = sensorValue * (5.0 / 1023.0);

  // Tampilkan tegangan di LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Tegangan: ");
  lcd.print(voltage);
  lcd.print("V");
  lcd.setCursor(0, 1);
  lcd.print("Sensor: ");
  lcd.print(sensorValue);
  lcd.print(" ADC");
  // Tentukan warna LED sesuai dengan rentang tegangan
  if (voltage >= 0.0 && voltage <= 0.6) {
    digitalWrite(redLedPin, HIGH);
    digitalWrite(yellowLedPin, LOW);
    digitalWrite(greenLedPin, LOW);
  } else if (voltage > 0.6 && voltage <= 1.2) {
    digitalWrite(redLedPin, LOW);
    digitalWrite(yellowLedPin, HIGH);
    digitalWrite(greenLedPin, LOW);
  } else if (voltage > 1.2 ) {
    digitalWrite(redLedPin, LOW);
    digitalWrite(yellowLedPin, LOW);
    digitalWrite(greenLedPin, HIGH);
  }

  delay(1000);  // Tunda selama 1 detik sebelum membaca ulang tegangan
}