I have created a small circuit with a 220 Ohm resistor and a LED connected to PIN9 for power.
I want to measure the Voltage of the current flowing through the circuit. In order to do that I connected a bridge from between the resistor and the LED to the A3 PIN and used this code:
int ledPin=9;
int inPin=A3;
long int in=0;
float V_in=0;
int val;
void setup() {
analogReference(DEFAULT);
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) {
val = Serial.parseInt();
if (val>=0 && val<=155) {
analogWrite(ledPin, val);
//Serial.println(val);
for (int i=0; i<10; i++) {
val+=5;
analogWrite(ledPin, val);
//Serial.println(val);
delay(1000);
in = analogRead(inPin);
V_in=float(5.0)*(float(in)/float(1023));
Serial.println(V_in);
delay(1000);
}
}
else {
Serial.println("Dio è morto.");
}
}
}
The problem is that when I run it, with an Input of 55, I get what seams to be a random arrangement of 0s and 2.56s. While I expect a increasing sequence of numbers between 0 and 2.5.
What am I doing wrong?
I measured the Voltage on the A3 Pin using a voltimeter, and it increases every step by 0.2, exactly as expected.
This is an output example:
analogWrite
function doesn't output an analog voltage. It can only output 5V or 0V, nothing in between. It's called PWM.