อยากเป็นนักประดิษฐ์ นักคิด นัก DIY Ep2

Published at : 23 Dec 2025

ออกแบบวงจรอ่านค่าแบตเตอรี่ 0-24V ด้วยบอร์ด Arduino Uno
อยากเป็นนักประดิษฐ์ นักคิด นัก DIY ต้องลงมือทำ
ระบบไฟเป็นหัวใจของงานอิเล็คทรอนิคส์ โดยเฉพาะสถานีที่อยู่ห่างไกล ทำงานด้วยระบบโซล่าร์เซลล์ หรือแบตเตอรี่ จำเป็นอย่างยิ่งที่ต้องมีการตรวจสอบค่าของแบตเตอรี่ และรายงานผลอยู่ตลอด

Source program
/*
Read Battery Voltage input
board Arduino Uno
created 21 Feb 2020
modified 21 Feb 2020
By Saroj Meesook
*/

const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogA1 = A1; // Analog input for read Battery voltage input
String inputString = ""; // a String to hold incoming data
String lcstr;
bool stringComplete = false; // whether the string is complete

void setup() {
// reserve 128 bytes for the inputString:
inputString.reserve(128);
// initialize serial communications at 9600 bps:
Serial.begin(9600);
Serial.println("Hello World");
Serial.println("Battery level measurement");
}

void loop()
{
if (stringComplete)
{
lcstr=inputString;
if (lcstr == "BV") {
Serial.print("Waiting..\r\n");
delay(250);
lcstr=ReadBat();
Serial.println(lcstr+"V");
}
else {
Serial.println("Command error");
}
inputString = ""; //Clear serial port buffer
stringComplete = false;
}
delay(100);
}

String ReadBat(){
int A1value = 0;
float Vb = 0.0;
char str[7]="";

A1value = analogRead(analogA1);
Vb = A1value * 0.00488 *10;
dtostrf(Vb, 5, 2, str); // 5=all of 5 digit, 2=decimal point
return(str);
}

//------------------------------------------------------------------------------
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
inputString.trim();
stringComplete = true;
}
}
}