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);
}
}
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.
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);
}
}