Link Ads

More search type here...

January 5, 2024

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!

March 5, 2023

Color sensor calibration TCS 3200

Calibrating a color sensor like the TCS 3200 typically involves a few steps to ensure accurate color readings. Here's a general outline of the calibration process:

  1. Clean the sensor: Before calibrating the sensor, make sure it is free from dust and debris that could affect its readings. Use a clean cloth or compressed air to remove any contaminants from the sensor surface.
  2. Choose a calibration surface: Next, select a surface to use as your calibration target. Ideally, this surface should be a neutral color (e.g. white or gray) and have a matte finish to minimize reflections. You may also want to choose a surface that is representative of the types of colors you'll be measuring with the sensor.
  3. Measure the target: Place the calibration target under the sensor and measure its color using the sensor's built-in LEDs. Record the RGB values for the target color.
  4. Calculate correction factors: Compare the measured RGB values to the expected values for the target color, and calculate correction factors to adjust the sensor's readings. For example, if the sensor consistently reads red as slightly brighter than expected, you could apply a correction factor to decrease the reported red value.
  5. Apply correction factors: Once you have calculated correction factors for each color channel, apply them to the sensor's readings. You may need to adjust the correction factors over time as the sensor's performance changes or you encounter different lighting conditions.

Note that the exact calibration process may vary depending on your specific sensor and application. Be sure to consult the manufacturer's documentation for detailed instructions and recommended best practices.



To calibrate the TCS3200 color sensor, you can follow these steps:

  1. First of all, connect the TCS3200 sensor to the Arduino board and install the basic programming code which will read the colors from the sensor and display them on the serial monitor.
  2. Place the sensor on a white surface, then read the RGB color values on the serial monitor. Make sure the RGB value on white is around (255,255,255). If not, you can adjust the gain value on the sensor to optimize color readability.
  3. After setting the gain, place the sensor on a black surface and read the RGB color values on the serial monitor. Make sure the RGB value on black is around (0,0,0).
  4. After adjusting white and black, you can use other colors for reference such as red, green, blue or other colors. Make sure the resulting RGB value is as expected.
  5. Finally, you can adjust the integration time (INTEG) on the sensor to optimize color rendering. The longer the integration time, the more accurate the color reading results, but the slower the data processing speed. You can try to time the integration from 2,4,8,16, and 32 clock cycles.

Here is an example of basic program code to read RGB values on the TCS3200 color sensor:

#include <Adafruit_TCS3200.h>
Adafruit_TCS3200 colorSensor(TCS3200_S2, TCS3200_S3, TCS3200_OUT);
void setup() {
  Serial.begin(9600);
  colorSensor.begin();
}
void loop() {
  colorSensor.setResolution(TCS3200_18BIT);
  colorSensor.writeRegister(0x00, TCS3200_MEASUREMENT_MODE);
  delay(50);
   uint16_t red = colorSensor.readRed();
  uint16_t green = colorSensor.readGreen();
  uint16_t blue = colorSensor.readBlue();
  Serial.print("RGB: ");
  Serial.print(red);
  Serial.print(" ");
  Serial.print(green);
  Serial.print(" ");
  Serial.println(blue);
}

You can calibrate the sensor by changing the gain and integration time in the following sections:

colorSensor.setGain(TCS3200_GAIN_1X);
colorSensor.setIntegrationTime(TCS3200_INTEGRATIONTIME_2_4MS);

If the color reading results are still not accurate, you can try adjusting the gain and integration time until the resulting RGB values are as expected.