Tuesday, March 13, 2012

Arduino + ULN2803

I got some ULN2803 NPN darlington arrays working with my Arduino. Its quite simple to setup, not much different than a NPN transistor. As you can see from the scheme below, i used the ULN2803 as a PWM for a motor and for a RGB LED strip. Code wise, its just simple analogWrite to change the PWM, or a digitalWrite.

The left side of the picture below shows the setup for a simple hobby motor @ 3v and about 825ma(V=IR, I=3.3/~4ohm) each darlington in the array can do about 500ma, so i just tied three together, and now i can use up to about 1.5A. I used the code(also below) to go back and forth, full to slow speed, with PWM and analogWrite on the Arduino.

The right side shows how i set it up for some RGB LED strips. They are 12v, common anode(+), and so i just fed it 12v on the "+" pin and connected each of the R, G, and B cathode(-) pins to darlington on the ULN2803. Again, works great with analogWrite.

Motor Setup

LED setup with motor left overs


RGB Code

#define DelayTime 10 //time between colors
#define FadeDelay 10 //time to spend on each PWM level

void setup()
{
  // put your setup code here, to run once:
  pinMode(5, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop()
{
  //red
  digitalWrite(5, HIGH);
  for(int x= 255; x>1; x--)
  {
    analogWrite(5,x);
    delay(FadeDelay);
  }
  delay(DelayTime);
  digitalWrite(5, LOW);
  //blue
  digitalWrite(3, HIGH);
  for(int x= 255; x>0; x--)
  {
    analogWrite(3,x);
    delay(FadeDelay);
  }
  delay(DelayTime);
  digitalWrite(3, LOW);
  //white
  digitalWrite(5, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(6, HIGH);
  for(int x= 255; x>1; x--)
  {
    analogWrite(5,x);
    analogWrite(3,x);
    analogWrite(6,x);
    delay(FadeDelay);
  }
  delay(DelayTime);
  digitalWrite(5, LOW);
  digitalWrite(3, LOW);
  digitalWrite(6, LOW);
}

Motor Code

#define delaytime 50

void setup()  {
  pinMode(3, OUTPUT);
  digitalWrite(3,LOW);
  delay(250);
}

void loop()  {
  for(int i =0; i<255; i++) //slow to fast
  {
    analogWrite(3, i);
    delay(delaytime);
  }
  delay(500); //full speed for .5sec
  for(int i =255; i>0; i--) //fast to slow
  {
    analogWrite(3, i);
    delay(delaytime);
  }                      
}


2 comments: