daniel23
Published © LGPL

Arduino controlled power supply for led string lights

Replacement for a 31V power adapter for LED string lights. Soft dimming and possibility to power several strings in parallel on each output.

IntermediateShowcase (no instructions)1,624
Arduino controlled power supply for led string lights

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
l298n dual h bridge module
×1

Story

Read more

Schematics

Alimentation Guirlande de Noël

Code

Power supply for LED christmas string lights

Arduino
const byte pin_1P = 3;    // these are the pins connected to the L298N driver
const byte pin_1N = 11;   // you can change it at your desire.
const byte pin_2P = 9;
const byte pin_2N = 10;

//unsigned int frequency = 150;
int halfPeriod = 3333;            // (500000 / frequency)
unsigned long currentMicros = 0;
unsigned long previousMicros = 0;

int Interval;
int defaultInterval = 5;
unsigned long previousInterval = 0;
unsigned long currentInterval = 0;

bool phase = true;                // true: positive alternation  -  false:  negative alternation
long positif = halfPeriod * 0.1;  // duration of positive alternation
long negatif = halfPeriod * 0.1;  // duration of negative alternation
int increment = 1;

void setup()
{
  pinMode(pin_1P, OUTPUT);     // LED_1 positive
  pinMode(pin_1N, OUTPUT);     // LED_1 negative
  pinMode(pin_2P, OUTPUT);     // LED_2 positive
  pinMode(pin_2N, OUTPUT);     // LED_2 negative
  Serial.begin(2000000);       // (115200);
  Interval = defaultInterval;
}

void loop()
{
  currentMicros = micros();
  if (currentMicros - previousMicros >= halfPeriod) {
    previousMicros = currentMicros;
    phase = !phase;
  }

  // Optionally insert the modifications of the "positive" and "negative" times to create effects

  if (positif >= halfPeriod) {
    increment = -1;
  }
  if (positif <=  0) {
    increment = 1;
  }
  if ((positif < 200) || (positif > (halfPeriod - 200))) {
    Interval = defaultInterval * 5;
  }
  else {
    Interval = defaultInterval;
  }
  currentInterval = millis();
  if (currentInterval - previousInterval >= Interval) {
    previousInterval = currentInterval;
    positif = positif + increment ;
    negatif = halfPeriod - positif;
  }
  if ((phase == true) && ((currentMicros - previousMicros) < positif)) {
    digitalWrite (pin_1N, LOW);
    digitalWrite (pin_1P, HIGH);
    digitalWrite (pin_2N, LOW);
    digitalWrite (pin_2P, HIGH);
  }
  else {
    digitalWrite (pin_1P, LOW);
    digitalWrite (pin_2P, LOW);
  }
  if ((phase == false) && ((currentMicros - previousMicros) < negatif)) {
    digitalWrite (pin_1P, LOW);
    digitalWrite (pin_1N, HIGH);
    digitalWrite (pin_2P, LOW);
    digitalWrite (pin_2N, HIGH);
  }
  else {
    digitalWrite (pin_1N, LOW);
    digitalWrite (pin_2N, LOW);
  }
}

Credits

daniel23

daniel23

7 projects • 10 followers

Comments