Can i read pwm from an output and serial print it?

Hello everyone,

im working on a rgb led to display different colours at random just to get familiar with the program.
now i would like to read the output from port 9 (red), 10(green) and 11(blue)

however i only read either the port number which is not what i want
or
i read the formula for "waarde" which works but if its not a led it would not tell me if the signal really goes trough the output

so i decided to use the read function, but i think its not working (see below in serialprint) because its an input function being asigned to an output, so what do i do here?

So how do i read the actual PWM value sent trough the port ?

appreciate the advice

Here is the code,

int ledPinRood=9;
int ledPinGroen=10;
int ledPinBlauw=11;
unsigned char waarde;
unsigned char waardeRood;
unsigned char waardeGroen;
unsigned char waardeBlauw;

void setup()
{
pinMode(ledPinRood,OUTPUT);
pinMode(ledPinBlauw,OUTPUT);
pinMode(ledPinGroen,OUTPUT);
Serial.begin(9600);
}
void loop()
{
waardeRood=analogRead(ledPinRood);
waardeGroen=analogRead(ledPinGroen);
waardeBlauw=analogRead(ledPinBlauw);
waarde=(random(0,255));

analogWrite(ledPinRood, waarde);
analogWrite(ledPinBlauw, waarde);
analogWrite(ledPinGroen, waarde);

Serial.print("rood ,");
Serial.print(waardeRood);
Serial.print(", ");
Serial.print("groen ,");
Serial.print(waardeGroen);
Serial.print(", ");
Serial.print("blauw ,");
Serial.print(waardeBlauw);
Serial.print(", ");
Serial.println(waarde);
delay(2500);

and the serial print that comes with it ;

16:09:59.530 -> rood ,255, groen ,212, blauw ,223, 87
16:10:02.047 -> rood ,17, groen ,230, blauw ,238, 40
16:10:04.572 -> rood ,13, groen ,228, blauw ,235, 232
16:10:07.065 -> rood ,29, groen ,251, blauw ,4, 122
16:10:09.538 -> rood ,212, groen ,200, blauw ,179, 39
16:10:12.033 -> rood ,66, groen ,54, blauw ,14, 47
16:10:14.551 -> rood ,60, groen ,36, blauw ,251, 60

There is no need to read it because you set it in the first place so why do you need to read it again?
As you always set the PWM to all LEDs as the variable waarde it is that value.

Post moved from the section about getting the IDE working to here.

analogRead() only works on analog input pins (A0 though A5). You can't use analogRead() on PWM output pins.

Since you are the one writing to the PWM pins, you already know the values:

void loop()
{
  waarde = (random(0, 255));

  analogWrite(ledPinRood, waarde);
  analogWrite(ledPinBlauw, waarde);
  analogWrite(ledPinGroen, waarde);

  Serial.print("rood ,");
  Serial.print(waarde);
  Serial.print(", ");
  Serial.print("groen ,");
  Serial.print(waarde);
  Serial.print(", ");
  Serial.print("blauw ,");
  Serial.print(waarde);
  Serial.print(", ");
  Serial.println(waarde);
  delay(2500);
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.