/*
GARRF Virtual Engineering Laboratory
EXPERIMENT 01
Ohm's Law & Virtual Electronics
Developed by:
Dr. Annamdas Venu Gopal Madhav
and
Shantanu Vasudev Krishna Annamdas
Product of GARRF
*/
const int POT_PIN = A0;
const int LED_PIN = 9;
const float SUPPLY_VOLTAGE = 5.0;
const float RESISTANCE_OHMS = 220.0;
void setup() {
pinMode(LED_PIN, OUTPUT);
Serial.begin(115200);
Serial.println();
Serial.println("======================================");
Serial.println(" GARRF VIRTUAL ENGINEERING LAB");
Serial.println(" EXPERIMENT 01 - OHM'S LAW");
Serial.println("======================================");
Serial.println();
Serial.println("Turn the virtual potentiometer.");
Serial.println("Observe Voltage, Current and Resistance.");
Serial.println();
}
void loop() {
int adcValue = analogRead(POT_PIN);
/*
Convert ADC reading to voltage.
Arduino UNO ADC = 10 bit = 0..1023
Reference voltage = 5V
*/
float voltage =
(adcValue / 1023.0) * SUPPLY_VOLTAGE;
/*
For teaching purposes we treat the
220-ohm resistor as the load.
*/
float current =
voltage / RESISTANCE_OHMS;
float current_mA =
current * 1000.0;
/*
LED brightness changes with the
potentiometer setting.
*/
int brightness =
map(adcValue, 0, 1023, 0, 255);
analogWrite(LED_PIN, brightness);
Serial.println("--------------------------------------");
Serial.print("ADC Value : ");
Serial.println(adcValue);
Serial.print("Voltage : ");
Serial.print(voltage, 3);
Serial.println(" V");
Serial.print("Resistance : ");
Serial.print(RESISTANCE_OHMS, 1);
Serial.println(" ohm");
Serial.print("Calculated I : ");
Serial.print(current_mA, 3);
Serial.println(" mA");
Serial.print("Ohm's Law Check : V = I x R = ");
Serial.print(current, 5);
Serial.print(" x ");
Serial.print(RESISTANCE_OHMS, 1);
Serial.print(" = ");
Serial.print(current * RESISTANCE_OHMS, 3);
Serial.println(" V");
delay(1000);
}