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
}



 

August 5, 2023

Arduino coding for display LCD 16x2 using I2C wire

 Arduino code to display text on a 16x2 LCD using the I2C wire library and an I2C LCD module (PCF8574):




First, make sure you have installed the "LiquidCrystal_I2C" library. If you haven't, you can download it from the Library Manager in the Arduino IDE.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// Set the LCD address (0x27 or 0x3F) according to your module
#define I2C_ADDR 0x27

// Set the LCD size (16x2)
#define LCD_COLUMNS 16
#define LCD_ROWS 2

// Create an instance of the LCD object
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_ROWS);

void setup() {
  // Initialize the LCD
  lcd.begin(LCD_COLUMNS, LCD_ROWS);

  // Turn on the backlight (if available)
  lcd.backlight();

  // Write text to the LCD
  lcd.print("Hello, World!");
}

void loop() {
  // Add your code here (if needed)
}


In this example, we use the "LiquidCrystal_I2C" library to simplify the communication between the Arduino and the I2C LCD module (PCF8574). The library provides functions like lcd.print(), lcd.setCursor(), lcd.clear(), and more to control the LCD easily.

Make sure you have connected the SDA and SCL pins of the I2C module to the corresponding pins on your Arduino. Also, don't forget to provide power and ground connections.

Before uploading the code to your Arduino, double-check the I2C address of your LCD module. If it's different from 0x27, update the line #define I2C_ADDR 0x27 with the correct address.

That's it! Now you should be able to see the "Hello, World!" message on your 16x2 LCD connected via I2C. Good luck with your project!