Serial Monitor reads LED PWM value and Photodiode Value

I'm trying to write a program that has an LED fading in/out using PWM and then have a photodiode reading out the value. I am using an Arduino Uno. So far, my photodiode code is all working correctly. I am getting the values I expect and they change depending on the LED. My issue, though, is getting a value of the LED brightness. Is there a way to have the serial monitor show the brightness of the LED in one column and the photodiode reading in another? This is my code so far:

//Initializing LED Pin
int led_pin = 6;
int sensorPin = A0;
int sensorValue = 0;
int ledValue = 0;
void setup() {
//Declaring LED pin as output
Serial.begin(9600);
pinMode(sensorPin, INPUT); //sensor input
pinMode(led_pin, OUTPUT);
}
void loop() {
//Fading the LED
for(int i=0; i<255; i++){
analogWrite(led_pin, i);
delay(5);
sensorValue = analogRead(sensorPin);
ledValue = digitalRead(led_pin);
Serial.println(sensorValue);
Serial.println(ledValue);
}
for(int i=255; i>0; i--){
analogWrite(led_pin, i);
delay(5);
sensorValue = analogRead(sensorPin);
ledValue = digitalRead(led_pin);
Serial.println(sensorValue);
Serial.println(ledValue);
}
}

Right now, the monitor looks like this:


The 0 is for when the LED is fading off and the 1 is for when it is fading on.

Instead of

Serial.println(sensorValue);
Serial.println(ledValue);

use

Serial.print(sensorValue);
Serial.print("   ");
Serial.println(ledValue);

Thank you so much! is there a way I can get the LED to read off the value of the PWM on a scale insead of just 0 vs. 1. Sorry if my wording is weird I am very new to arduino.

Serial.print(sensorValue);
Serial.print("\t");
Serial.print(ledValue);
Serial.print("\t");
Serial.println(i);

i like my columns to be lined up regardless of the number of digits

    char s [80];
    sprintf (s, " %d %6d", ledValue, sensorValue)
    Serial.println (s);

I think the data will be easier to understand if you display it on Serial Plotter. I would divide the sensorValue by 4 to get both signals in the 0-255 range.

//Initializing LED Pin
const int led_pin = 6;  // Must be PWM pin
const int sensorPin = A0;

void setup()
{
  //Declaring LED pin as output
  Serial.begin(9600);
  pinMode(sensorPin, INPUT); //sensor input
  pinMode(led_pin, OUTPUT);
}

void loop()
{
  //Fading the LED
  for (int i = 0; i < 255; i++)
  {
    analogWrite(led_pin, i);
    delay(5);
    int sensorValue = analogRead(sensorPin);
    Serial.print(sensorValue / 4);
    Serial.print(',');
    Serial.println(i);
  }
  for (int i = 255; i > 0; i--)
  {
    analogWrite(led_pin, i);
    delay(5);
    int sensorValue = analogRead(sensorPin);
    Serial.print(sensorValue / 4);
    Serial.print(',');
    Serial.println(i);
  }
}

Tools > Serial Plotter

Hadn't noticed.
How long has that been around?

Only about 7 years. :slight_smile:

Release notes:
ARDUINO 1.6.6 - 2015.11.03
* Added Serial Plotter. Thanks @henningpohl

A post was split to a new topic: Interact the LED with photo diode

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