Author Topic: Digispark and RGB LED Strip + code question  (Read 12496 times)

togethernessnyc

  • Newbie
  • *
  • Posts: 10
Digispark and RGB LED Strip + code question
« on: April 24, 2013, 07:50:44 pm »
Hi!


I'm working on my first project with the Digispark.  The project includes four 48" dowels inset with an RGB LED, proximity sensor, and Digispark.  I am programming the RGB LED strip and will leave the Digisparks in the dowels permanently. 


First, I was wondering if I could get some help with the code.  The effect I am trying to create is as follows:  RGB LED strip "breathing" in a blue monochrome.  When person enters range of proximity sensor, the LED strip breathes at the same rate, but brightens increasingly as proximity increases. 


Here is what I've been working with: 


Code: [Select]
[/quote]#include <SPI.h>




int sensorPin = A3;        // the pin that the sensor is attached to
int LEDpin = 9;           // the pin that the LED is attached to
int proximity;
int pwmActual[] = {
  0, 1, 2, 3, 4, 5, 7, 9,
  12, 15, 18, 22, 27, 32, 37, 44,
  50, 58, 66, 75, 85, 96, 107, 120,
  133, 147, 163, 179, 196, 215, 234, 255
};
float counter;
float val;
int minMap, maxMap;


// the setup routine runs once when you press reset:
void setup()  {
  // declare pin 9 to be an output:
  pinMode(LEDpin, OUTPUT);
  Serial.begin(9600);
}


// the loop routine runs over and over again forever:
void loop()  {


  proximity = analogRead(sensorPin);   // returns 0 to 1023 in theory (0-5V), but TEST SENSOR
//  Serial.println(proximity);


  minMap  = map(proximity, 0,1023, 0,45);
 
  maxMap = minMap + 128;


counter += .1;
val = sin(counter) * 1000;




val = map(val, -1025,1025, minMap, maxMap);
analogWrite(LEDpin, val);


//Serial.println(sin(counter));
Serial.println(maxMap);
 
  // wait for 30 milliseconds to see the dimming effect   
  delay(30);                           
}


[quote]


Right now, I'm very close, but it needs to be tweaked.  I'd like for the peak brightness to be much lower when the viewer is out of range of the sensor.  I'm okay with the rest of it.  Can anyone help? 


I haven't used the Digispark at all yet, and I'm also still pretty new to Arduino in general, so no advice is too basic.  I plan on searching the website now for any libraries, etc., that will be necessary for the software to play nicely with Digispark, but feel free to suggest anything that third-party users have created that I might not be aware would help. 


As far as physical construction, the LED strip requires 12v.  I'll be powering with 8 AAs in series, and using a 5v voltage regulator from power to the Digispark.  Then, from Digispark to LED strip, I'll be following the Adafruit tutorial here: 


http://learn.adafruit.com/rgb-led-strips/usage


Does anyone foresee any issues with this, or does it look pretty good? 


I should note, in case it's not clear from the code, that up until now, I have been testing the code with a single LED . . . not with the strip. 


Thanks!
« Last Edit: April 24, 2013, 07:52:20 pm by togethernessnyc »

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Digispark and RGB LED Strip + code question
« Reply #1 on: April 25, 2013, 02:39:45 am »
Re code: Remove the include for SPI.h - not needed and won't work with the Digi - comment out any Serial lines, and changes the pins as appropriate - the rest looks like it should work fine


Re power: You can feed the Digispark the 12v on the VIN pin, no need to use a 5v regulator the VIN pin goes to the onboard 5V regulator.

togethernessnyc

  • Newbie
  • *
  • Posts: 10
Re: Digispark and RGB LED Strip + code question
« Reply #2 on: April 25, 2013, 10:00:15 am »
@digistump, re:  12v, thanks!  great tip :) i should have some documentation within the next week or two . . . i'll be sure to post on the forum.   

togethernessnyc

  • Newbie
  • *
  • Posts: 10
Re: Digispark and RGB LED Strip + code question
« Reply #3 on: April 25, 2013, 04:03:55 pm »
hey!


i wanted to upload my final code.  hopefully the digispark can handle it!  although i don't see why it wouldn't be able to. 


thanks again!


Code: [Select]
int sensorPin = A0;        // the pin that the sensor is attached to
int LEDpin = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by
int proximity;
int maxBrightness;
float counter;
float val;
int minMap, maxMap;


// the setup routine runs once when you press reset:
void setup()  {
  // declare pin 9 to be an output:
  pinMode(LEDpin, OUTPUT);
  Serial.begin(9600);
 
  counter = 3*PI/2.0;
}


// the loop routine runs over and over again forever:
void loop()  {


  counter += .08;
  if(counter >= (7*PI/2.0)) {
    // Change third parameter here to change far range
    proximity = map(analogRead(sensorPin), 0, 250, 255, 0);
    proximity = pow(proximity, 3.0) / 65536.0;
    counter = 3*PI/2.0;
  }
   
  val = (sin(counter) + 1) * (proximity / 2.0);
  if(val > 255)
    val = 255;


  analogWrite(LEDpin, val);


  delay(30);                           
}



togethernessnyc

  • Newbie
  • *
  • Posts: 10
Re: Digispark and RGB LED Strip + code question
« Reply #4 on: April 26, 2013, 07:38:03 pm »
After some delays, I've finally read this page: 


http://digistump.com/wiki/digispark/tutorials/connecting


It's made my slightly afraid of potentially frying my Digisparks. 


Has anyone attempted connecting it to an RGB LED strip yet?  I was wondering if someone might care to share a Fritzing translating to Digispark the wiring diagrams from this link: 


http://learn.adafruit.com/rgb-led-strips/usage


A glaring omission from the Adafruit wiring diagram is power.  The only way I could get this to actually work with a regular Arduino is if I powered the LED strip directly via the breadboard.  Power directly into Arduino Uno, and out through Vin did not light the LED Strip. 


So if anyone has tried this, please share whatever you can!  Thanks :)

Mark

  • Full Member
  • ***
  • Posts: 196
Re: Digispark and RGB LED Strip + code question
« Reply #5 on: April 27, 2013, 12:46:11 am »
Is this any help.
Its using the transistor option.

mark

togethernessnyc

  • Newbie
  • *
  • Posts: 10
Re: Digispark and RGB LED Strip + code question
« Reply #6 on: April 28, 2013, 07:09:42 pm »
@Mark,


This helps a lot!  It confirms/re-iterates the Adafruit link I posted, but what I was really looking for is confirmation that the same setup will work with the digispark.  So if that's the case, as per this schematic, then thanks so much!




Mark

  • Full Member
  • ***
  • Posts: 196
Re: Digispark and RGB LED Strip + code question
« Reply #7 on: April 29, 2013, 12:18:12 am »
Looking at the WIKI it doesn't indicate any particular pin can't do PWM.
I had a quick look at the datasheet for the ATtiny85 and it also disn't give me a clue.
 
Suggest to be sure try it with a normal LED (PLUS RESISTOR), bearing in mind P5 is 3v not 5v output.
 
Mark

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Digispark and RGB LED Strip + code question
« Reply #8 on: April 29, 2013, 10:28:33 am »
p0, p1, p4 all can do pwm

togethernessnyc

  • Newbie
  • *
  • Posts: 10
Re: Digispark and RGB LED Strip + code question
« Reply #9 on: May 07, 2013, 05:25:13 pm »
@digitstump, you mentioned that digispark could be powered 12v through VIN.  However, I can't seem to find a way to power the digispark and LED strip from the same power source, simultaneously, via the digispark. 


I checked out this video, and although he's got it working, the power is going through the breadboard and out into the LEDs and digispark separately. 

http://www.youtube.com/watch?v=6XWSAGRGLpU

I've been referring to the Adafruit RGB LED strip tutorial as my model: 


http://learn.adafruit.com/rgb-led-strips/usage

Is there a way to send power into the digispark, through VIN, and out into the RGB using this circuit, since digispark only has one ground? 





digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
Re: Digispark and RGB LED Strip + code question
« Reply #10 on: May 07, 2013, 11:19:51 pm »
First of all - you can't power the LED strip from the Digispark - the Digispark can control the transistors or mosfets being used to power the LED strip, but it is just controlling how much power they allow.


Going off of the Adafruit tutorial you linked to


+12V goes to VIN on the Digispark and 12v on the LED Strip


GND goes to GND on the Digispark and Source pins of the mosfets


The digispark pins go to the base pins of the mosfets (through resistors)


The R,G and B lines on the LED Strip go to the drain pins on the mosfets


togethernessnyc

  • Newbie
  • *
  • Posts: 10
Re: Digispark and RGB LED Strip + code question
« Reply #11 on: May 08, 2013, 05:13:52 am »
Sorry, I'm still a little unclear on this. 


Quote
+12V goes to VIN on the Digispark and 12v on the LED Strip

GND goes to GND on the Digispark and Source pins of the mosfets
Do you mean I have two wires going to the same pin on the digi?  i.e., a connection from 12V to digi on VIN, and a separate wire frpm digi to strip, also on VIN?
« Last Edit: May 08, 2013, 07:49:46 am by togethernessnyc »

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Digispark and RGB LED Strip + code question
« Reply #12 on: May 08, 2013, 10:51:06 am »
Your 12V power source would be powering the Digispark *and* the LED strip. With a common ground from the power source to both the Digispark and the MOSFET source.


Don't think of the power as going "through" the Digispark. Power is going through the MOSFETs to your LED strip. The Digi is just controlling the on/off switch for the power (the MOSFET). It's simply a convenience that you can power the spark off the same 12V source. You *could* power the spark separately, off a low-voltage source, wire the 12V+ to the LED strip, and then just connect the grounds of the low and high voltage sources.


Make sense?


togethernessnyc

  • Newbie
  • *
  • Posts: 10
Re: Digispark and RGB LED Strip + code question
« Reply #13 on: May 08, 2013, 11:07:15 am »
Hi @dougal, thanks! 


Yes, it makes sense in theory, but what I have been trying to work out is the physical build. 


To make it simple:  I have a 12v power source with power and ground coming off it, a single red and black hookup wire, respectively. 


I've been told that to make this work, I have to solder an extra wire onto each of those hookups.  In other words, coming off of the 12v power supply, I will have a red cable that looks like the letter "Y", and a black cable that also does. 


Does this sound accurate? 

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Digispark and RGB LED Strip + code question
« Reply #14 on: May 08, 2013, 11:23:35 am »
Yes, you essentially want to 'Y' the connections.


It doesn't have to physically look like a Y, as long as the electrical connections go to the right places.


Power source +12V -> Digispark VIN *and* LED strip V+
Power source GND -> Digispark GND *and* MOSFET source
MOSFET drain -> LED strip R/G/B signal