sorry for the lack of details and not giving proper responses to your suggestions. It's not because I ignore them or lazy to provide details. It's the first circuit I'm building in my life, the first time I'm drawing schematics and the first time I'm using Eagle - so simply don't have enough experience. Again, sorry for that - I appreciate your time and trying to help! And it's my bad I didn't post details about LED circuit.
How I expect everything to work:
- Arduino is powered by the external 9V wall socket
- External LED panel is hooked up the Vin pin to have enough power
- Relay is controlled by arduino digital pin to break negative - switch the led on/off
- the array of transistors is my attempt to programmatically control resistance on LED panel loop and by that control brightness of the light.
- The first transistor is used to skip or not the rest of transistors+resistors as even "open" transistors significantly reduce the brightness.
- At some point the LEDs were flickering and I thought it's because of the unstable power supply, so I added a couple of capacitors in front of the LED circuit. Not sure if it was bad contacts and not sure if those capacitors are needed, but he flickering is gone now.
The prototype on my first schema works. The brightness, as aarg rightly said, is not linear, but still works. It's possible my schematics are not correct - again, that's my first time doing it and I struggle a lot Let me try to revisit it and correct.
PWM: the way I tried it is hooked up analog output to the transistor to base and LED panel to emitter. I did analog write 0-255 on base pin and the LED panel only lights up on 255 value. I guess this is completely wrong?
UPDATE: I will also attach my code, maybe it will do better job at explaining the logic than I do:
#include <Wire.h>
#include <DS3231.h>
#include <IRremote.hpp>
#define IR_RECEIVE_PIN 2
#define RELAY_PIN 8
#define RESISTOR1_PIN 3
#define RESISTOR2_PIN 4
#define RESISTOR3_PIN 5
#define RESISTOR4_PIN 6
#define RESISTOR5_PIN 7
#define DIMMER_LEVEL_MAX 5
DS3231 myRTC;
DateTime dt;
uint8_t dimmer_level;
// schedule to switch the light ON. Format HHMMSS
long switch_on[] = { 73000, 170000 };
// Schedule to switch the lights off. Format HHMMSS
long switch_off[] = { 113000, 230000 };
void setup() {
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK);
pinMode(RELAY_PIN, OUTPUT);
Serial.println("Initialize RTC module");
Wire.begin();
DateTime dt = RTClib::now();
Serial.print("- DS3231 module init year: ");
Serial.println(dt.year());
if (dt.year() == 2000) {
Serial.println("- Setting module's date time to compile date:");
Serial.print(" ");
Serial.print(__DATE__);
Serial.print(" ");
Serial.println(__TIME__);
static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
static char buff[4] = { '0', '0', '0', '0' };
int y, m, d, hh, mm, ss;
sscanf(__DATE__, "%s %hhu %d", buff, &d, &y);
m = (strstr(month_names, buff) - month_names) / 3 + 1;
sscanf(__TIME__, "%hhu:%hhu:%hhu", &hh, &mm, &ss);
myRTC.setClockMode(false); // set to 24h
myRTC.setYear(y - 2000);
myRTC.setMonth(m);
myRTC.setDate(d);
myRTC.setHour(hh);
myRTC.setMinute(mm);
myRTC.setSecond(ss);
}
set_dimmer();
}
int inarray(long val, long arr[]) {
int i;
for (i = 0; i < sizeof(arr); i++) {
if (arr[i] == val)
return 1;
}
return 0;
}
void translateIR() {
switch (IrReceiver.decodedIRData.command) {
case 0x45:
Serial.println("ON");
digitalWrite(RELAY_PIN, HIGH);
break;
case 0x46: Serial.println("TIMER"); break;
case 0x47:
Serial.println("OFF");
digitalWrite(RELAY_PIN, LOW);
break;
case 0x44: Serial.println("1"); break;
case 0x43: Serial.println("2"); break;
case 0x7: Serial.println("3"); break;
case 0x9: Serial.println("4"); break;
case 0x16:
Serial.println("5");
break;
case 0xD:
Serial.println("6");
break;
case 0xC:
Serial.println("7");
break;
case 0x5E:
Serial.println("8");
break;
case 0x8:
Serial.println("DOWN");
dimmer_level = dimmer_level == DIMMER_LEVEL_MAX ? DIMMER_LEVEL_MAX : ++dimmer_level;
Serial.print("Setting dimmer level to ");
Serial.println(dimmer_level);
set_dimmer();
break;
case 0x5A:
Serial.println("UP");
dimmer_level = dimmer_level == 0 ? 0 : --dimmer_level;
Serial.print("Setting dimmer level to ");
Serial.println(dimmer_level);
set_dimmer();
break;
default: Serial.println(" other button ");
}
delay(500); // Do not get immediate repeat
}
void set_dimmer() {
switch (dimmer_level) {
case 0:
set_dimmer_resistors(HIGH, LOW, LOW, LOW, LOW);
break;
case 1:
set_dimmer_resistors(LOW, HIGH, HIGH, HIGH, HIGH);
break;
case 2:
set_dimmer_resistors(LOW, LOW, HIGH, HIGH, HIGH);
break;
case 3:
set_dimmer_resistors(LOW, LOW, LOW, HIGH, HIGH);
break;
case 4:
set_dimmer_resistors(LOW, LOW, LOW, LOW, HIGH);
break;
case 5:
set_dimmer_resistors(LOW, LOW, LOW, LOW, LOW);
break;
default: Serial.println("Error: unknown dimmer level");
}
}
void set_dimmer_resistors(uint8_t res1, uint8_t res2, uint8_t res3, uint8_t res4, uint8_t res5) {
digitalWrite(RESISTOR1_PIN, res1);
digitalWrite(RESISTOR2_PIN, res2);
digitalWrite(RESISTOR3_PIN, res3);
digitalWrite(RESISTOR4_PIN, res4);
digitalWrite(RESISTOR5_PIN, res5);
}
void loop() {
if (IrReceiver.decode()) {
translateIR();
IrReceiver.resume();
}
DateTime dt = RTClib::now();
long currenttime = (long)dt.hour() * 10000 + (long)dt.minute() * 100 + dt.second();
if (inarray(currenttime, switch_on)) {
digitalWrite(RELAY_PIN, HIGH);
}
if (inarray(currenttime, switch_off)) {
digitalWrite(RELAY_PIN, LOW);
}
}