// Give me arduino nano code
// inputVoltageSensorPotPin = A1
// outputVoltageSensorPotPin = A0
// Connected 16x2 I2C lcd
// ServoMotorPin = D3
// int setVoltage = 230 // V AC
// int servoSteps = 1 // degree steps
// ready the inputPot : range from 0 to 270V AC
// ready the outputPot : range from 0 to 270V AC
// Display it on the LCD
// condition if the inputPotValue is < setVoltage rotate the servo motor by steps = servoSteps increment
// condition if the inputPotValue is > setVoltage rotate the servo motor by steps = servoSteps decrement
// No
// the output voltage should be in range + and - : setVoltage
// and if the output voltage is less then rotate the servo clockvise to increase voltage else rotate anit clockvise to decre the volatge
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// ------------------ Pin Definitions ------------------
const int inputVoltageSensorPotPin = A1;
const int outputVoltageSensorPotPin = A0;
const int servoMotorPin = 3; // D3
// ------------------ LCD Setup ------------------
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16x2 LCD
// ------------------ Servo Setup ------------------
Servo servoMotor;
int servoPosition = 90; // initial servo position (middle)
// ------------------ Voltage & Servo Settings ------------------
const int setVoltage = 230; // target voltage in VAC
const int servoSteps = 1; // steps to rotate servo per loop
// ------------------ Function to map analog to voltage ------------------
float readVoltage(int analogPin) {
int analogValue = analogRead(analogPin); // 0-1023
float voltage = map(analogValue, 0, 1023, 0, 270); // map to 0-270V AC
return voltage;
}
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
lcd.clear();
// Initialize Servo
servoMotor.attach(servoMotorPin);
servoMotor.write(servoPosition);
// Initial display
lcd.setCursor(0, 0);
lcd.print("InputV: ");
lcd.setCursor(0, 1);
lcd.print("OutputV: ");
}
void loop() {
// Read voltages
float inputVoltage = readVoltage(inputVoltageSensorPotPin);
float outputVoltage = readVoltage(outputVoltageSensorPotPin);
// Display on LCD
lcd.setCursor(8, 0);
lcd.print(inputVoltage, 1);
lcd.print("V ");
lcd.setCursor(8, 1);
lcd.print(outputVoltage, 1);
lcd.print("V ");
// Control Servo based on output voltage relative to set voltage
if (outputVoltage < setVoltage && servoPosition < 180) {
// Rotate clockwise to increase output voltage
servoPosition += servoSteps;
if (servoPosition > 180) servoPosition = 180;
servoMotor.write(servoPosition);
}
else if (outputVoltage > setVoltage && servoPosition > 0) {
// Rotate counter-clockwise to decrease output voltage
servoPosition -= servoSteps;
if (servoPosition < 0) servoPosition = 0;
servoMotor.write(servoPosition);
}
delay(100); // small delay for stability
}