Categories

Digispark RGB led example

In this example we will connect an RGB led to a Digispark, a quick recap of the basic Digispark we ware using in this example


XGHF-GY Digispark Kickstarter Miniature Development Board TINY85 – Blue – $4.65

The Digispark is an Attiny85 based microcontroller development board similar to the Arduino line only cheaper smaller and a bit less powerful. With a whole host of shields to extend its functionality and the ability to use the familiar Arduino IDE the Digispark is a great way to jump into electronics or perfect for when an Arduino is too big or too much.

The Digispark is shipped fully assembled except for the two included and easy to solder headers.

Here are the specs: Support for the Arduino IDE 1.0+ (OSX/Win/Linux) Power via USB or External Source – 5v or 7-35v (12v or less recommended automatic selection)
On-board 500ma 5V Regulator
Built-in USB
6 I/O Pins (2 are used for USB only if your program actively communicates over USB otherwise you can use all 6 even if you are programming via USB)
8k Flash Memory (about 6k after bootloader)
I2C and SPI
PWM on 3 pins (more possible with Software PWM)
ADC on 4 pins
Power LED and Test/Status LED

Schematics

Here are some schematics and a breadboard layout

Digispark and RGB LED schematic

Digispark and RGB LED schematic

 

Here is the breadboard layout

Digispark and RGB LED breadboard

Digispark and RGB LED breadboard

 

Code

#define  redpin            1 
#define  greenpin            2
#define  bluepin            3
 
void setup() 
{ 
  // initialize the digital pin as an output.
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  // Set high for common anode type
  digitalWrite(redpin, HIGH);  
  digitalWrite(greenpin, HIGH);
  digitalWrite(bluepin, HIGH);
}
 
 
void loop() 
{
  digitalWrite(redpin, LOW);  //red on
  delay(500);
  digitalWrite(redpin, HIGH); //red off
  digitalWrite(greenpin, LOW); //green on 
  delay(500);
  digitalWrite(greenpin, HIGH); //green off
  digitalWrite(bluepin, LOW);  //blue on
  delay(500);
  digitalWrite(bluepin, HIGH); //blue off
}

 

Links
XGHF-GY Digispark Kickstarter Miniature Development Board TINY85 – Blue

USB Interface Digispark Kickstarter ATTINY85 Development Board – Black

Share

Infra-red control and RGB led example

In this example we connect an infra red receiver to our Arduino and an RGB led. We will use various button combinations on the remote control to switch on an LED and we will use a button to switch them all off as well.

Firstly lets take a look at the kit we needed for this example to connect to our arduino and the remote control.

1. The IR breakout – low cost and easy to connect to your Arduino. This consists of the IR receiver and required additional circuitry, I used the breakout but you could build it on a breadboard if you desired

eb-infrared

eb-infrared

2. The RGB LED – again in the form of a breakout with resistors on the breakout, this was a common anode type

eb-rgb breakout

eb-rgb breakout

3. A remote control – these usually come as part of microcontroller kits but again you can find these online

Remote control

Remote control

Lets take a look at the schematic of what you will build

Schematic

IR and RGB LED schematic

IR and RGB LED schematic

Once connected I observed that the power LED was lit on the IR breakout , so lets plan what we are doing here

Button 1 – Switch on Red lED
Button 2 – Switch on Green LED
Button 3 – Switch on Blue LED
Off Button – Switch off All LEDs

Now the first thing you need to do is upload the sketch below and discover what the codes are when you press a button on the remote control, each button is different and different types of remote controls are different as well.

There is a IR library to download and install, you can download it from here. A usual download and copy this into your Arduino -> Libraries folder. Start the IDE and enter the following

Code

#include <IRremote.h>

int receiver = 3; // pin 1 of IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results;

void setup()
{
Serial.begin(9600); // for serial monitor output
irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
if (irrecv.decode(&results)) // have we received an IR signal?
{
Serial.println(results.value, HEX); // display it on serial monitor in hexadecimal
irrecv.resume(); // receive the next value
}
}

Upload the sketch and open the Serial monitor, press the desired keys and check the output. You should see something like this displayed

FF30CF
FFFFFFFF
FF18E7
FFFFFFFF
FF7A85
FFFFFFFF
FFA25D
FFFFFFFF

Wait a minute, thats two sets of hexadecimal values per button press, well the FFFFFFFF actually means that the button was held down, in our code we would want to ignore that and use the other values. This gives us the following combinations for our remote control

Button 1 – FF30CF
Button 2 – FF18E7
Button 3 – FF7A85
Off Button – FFA25D

Now for the sketch to switch on and off the RGB led. Upload this sketch

Code

#include <IRremote.h>

int RECV_PIN = 3;
int REDLED = 6;
int GREENLED = 7;
int BLUELED = 8;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
pinMode(REDLED,OUTPUT);
pinMode(GREENLED,OUTPUT);
pinMode(BLUELED,OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
if (irrecv.decode(&results))
{
Serial.println(results.value, HEX);
translateIR();
for (int z=0; z<2; z++) // ignore 2nd and 3rd signal repeat
{
irrecv.resume(); // receive the next value
}
}
}

void translateIR() // takes action based on IR code received
// describing Sony IR codes on LCD module
{
switch(results.value)
{
case 0xFF30CF:
digitalWrite(REDLED,LOW);
break;
case 0xFF18E7:
digitalWrite(GREENLED,LOW);
break;
case 0xFF7A85:
digitalWrite(BLUELED,LOW);
break;
case 0xFFA25D:
digitalWrite(REDLED,HIGH);
digitalWrite(GREENLED,HIGH);
digitalWrite(BLUELED,HIGH);
break;
default:
Serial.println("OTHER");
}
delay(500);
}

Lets see a picture of this in action

ir and rgb blue

ir and rgb blue

Share

RGB LED example

This was a quick test for a multi color LED. Pins 2, 3 and 4 were used

 

int red = 2;
int green =3;
int blue =4;
int j;

void setup()
{
pinMode(red,OUTPUT);
pinMode(green,OUTPUT);
pinMode(blue,OUTPUT);
}

void loop()
{
for (j = 1; j < 10000; j++)
{
analogWrite(red,random(255));
delay (random(10,31));
analogWrite(green,random(255));
delay (random(10,31));
analogWrite(blue,random(255));
delay (random(10,31));
}
}
Share

Arduino and multi-color LED example

In the example I used an RGB LED breakout. The LED was wired up as follows

5v – 5v
R – Pin 4
G – Pin 5
B – Pin 6

Here is a picture of the RGB LED

rgb led

rgb led

Here is a rough schematic

RGB LED

RGB LED

 

 

Code

Cycle through the LED colours

int green = 5;
int blue = 6;

// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
pinMode(red, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(red, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(red, HIGH); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(green, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(green, HIGH); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(blue, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(blue, HIGH); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

Switch them all on at the same time

int red = 4;
int green = 5;
int blue = 6;

// the setup routine runs once when you press reset:
void setup()
{
// initialize the digital pin as an output.
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
digitalWrite(red, LOW); // turn the LED on (LOW is the voltage level)
digitalWrite(green, LOW); // turn the LED on (LOW is the voltage level) // wait for a second
digitalWrite(blue, LOW); // turn the LED on (LOW is the voltage level)
delay(1000); // wait for a second
digitalWrite(red, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(green, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(blue, HIGH); // turn the LED off by making the voltage HIGH
delay(1000); // wait for a second
}

Links

Here are links to the sensor kit

50 Pcs 5mm Diameter Frosted Head 4 Pins RGB Light LED Diodes – Amazon UK
50 Pcs 5mm Round Head Common Cathode RGB Light LED Emitting Diodes – Amazon US

Share