#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
const int ledPin = 9;
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
pinMode(ledPin, OUTPUT);
}
void loop() {
DateTime now = rtc.now();
int luxValue = getLuxValue(now.hour(), now.minute());
int pwmValue = map(luxValue, 0, 450, 0, 255);
analogWrite(ledPin, pwmValue);
delay(60000);
}
int getLuxValue(int hour, int minute) {
if (hour >= 6 && hour < 8) return 75;
if (hour >= 8 && hour < 12) return 250;
if (hour >= 12 && hour < 16) return 450;
if (hour >= 16 && hour < 18) return 250;
if (hour >= 18 && hour < 22) return 75;
return 15;
}
``` I wrote a code that gives different light intensity at certain times of the day.

My circuit is like this but the LED does not light up and when I connect the voltmeter it shows 0 volts. What could be the reason?
You don't have a current limiting resistor for the LED.
Connect it to which two points?
Once you are sure about the current limiting, and you know you have a good LED, you could first test your wiring with the blink sketch modifed to use pin9
Doesn't mosfet count? There is no current flowing through the LED. When I run the simulation, the LED should light up but it does not light up?
Sorry, I didn't realise you were not using a real Arduino. The mosfet doesn't count as a current limiting resistor because when it's on it's resistance is too low to limit the current.
Does the blink sketch work on the simulator using pin9 and your mosfet / LED?
Also,
use else for line return 15
and since you have serial available add some serial print for debugging.
#include <Wire.h>
#include <RTClib.h>
RTC_DS1307 rtc;
const int ledPin = 9;
void setup() {
Serial.begin(9600);
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}
if (!rtc.isrunning()) {
Serial.println("RTC is not running, setting the time.");
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
} else {
Serial.println("RTC is running.");
}
pinMode(ledPin, OUTPUT);
}
void loop() {
DateTime now = rtc.now();
int luxValue = getLuxValue(now.hour(), now.minute());
int pwmValue = map(luxValue, 0, 450, 0, 255);
analogWrite(ledPin, pwmValue);
Serial.print("Current Time: ");
Serial.print(now.hour());
Serial.print(":");
Serial.print(now.minute());
Serial.print(":");
Serial.println(now.second());
Serial.print("Lux Value: ");
Serial.println(luxValue);
Serial.print("PWM Value: ");
Serial.println(pwmValue);
delay(60000);
}
int getLuxValue(int hour, int minute) {
if (hour >= 6 && hour < 8) return 75;
if (hour >= 8 && hour < 12) return 250;
if (hour >= 12 && hour < 16) return 450;
if (hour >= 16 && hour < 18) return 250;
if (hour >= 18 && hour < 22) return 75;
return 15;
}
``` Is it this way?
else {
return 15;
}
When I added a thank you resistor, the LED turned on. I missed that part. Do you think my circuit connections are correct now?
I assume "thank you resistor" was meant to be "current limiting resistor". I don't know if your circuit is correct now. If you intend to move over to using a real arduino then the value of the resistor will have to match the current capability of your LED. If you are only going to use the simulator, and it works in the simulator, then it's right enough ![]()
Hi, @ceyranci4141
Have you changed the simulator circuit to include a series current limit resistor in the LED circuit?
If so then can you post it in a new post.
PLEASE do not go back and edit old posts as you change your project.
Have you used an ammeter in your simulation to measure the current through the LED?
Thanks.. Tom...
![]()
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
