Testing RGB LEDs and the KY009 Three Colour LED.

The code is available on my GitHub page here.

Some versions of this board have resisistors in series with the LEDs, some do not. If resistors are not present then use a 390R resistor in series to limit the current.

I was not very impressed with this RGB LED board. It is nothing more than a three colour LED on a PCB, which is not a problem, however neither the Blue or the Red LEDs would work with PWM. This means that it’s not possible to achieve full colour mixing, although some limited mixing can be achieved. I tried a second board and on this one the Red and Blue LEDs would dim with PWM, but the green LED simply switched on and off.

I decided to write a sketch that could be used to test RGB LEDs with an Arduino. This sketch has two parts, the first switches LEDs on and off to provide a rainbow of colours, Red, Green, Yellow, Cyan, Blue and Violet. The second part tests the LEDs ability to fade in and out and each LED is tested separately. Either of the two routines can be switched off by setting either Of the variables rainbow or fader to false.

The LED in its naked form does not give very good colour mixing, so I made a simply diffuser by first cutting a ping pong ball in half then glueing some thin greasproof paper onto the open part of the ball. Place this dome (paper side down) over the RGB LED. An example of the diffuser is shown here.

Connect the common pin to pin 2, the Red LED to pin 3, the Green LED to pin 4 and the Blue LED to pin 5.



/*
RGB LED Test
This sketch checks a RGB LED to ensure that PWM can be used
to produce a complete range of colours
For best colour mixing results use a diffuser
*/
int gndPin = 2; // the RGB LED common connection
int redLed = 3; // the Red LED
int greenLed = 4; // the green LED
int blueLed = 5; // the blue LED
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
//
// set either of these to false to disable the effect
boolean rainbow = true; // enable the rainbow display
boolean fader = true; // enable the fade display
//
void setup() {
// declare pin 2 and 3 to be an output:
pinMode(gndPin, OUTPUT);
digitalWrite(gndPin, LOW);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(blueLed, OUTPUT);
}

void loop(){
if (rainbow){
// Rainbow

for(int r=0; r<3; r++){
analogWrite(redLed, 255); // Red
delay(2000);
analogWrite(greenLed, 255); // Yellow
delay(2000);
analogWrite(redLed, 0); // Green
delay(2000);
analogWrite(blueLed, 128); //Blue/Green
delay(2000);
analogWrite(blueLed, 255); //Blue/Green
analogWrite(greenLed, 0); //Blue
delay(2000);
analogWrite(redLed, 255); // Violet
delay(2000);
analogWrite(redLed, 0); // LEDs all OFF
analogWrite(blueLed, 0);
}
}
// the fader effect
if(fader){
brightness = 0;
fadeAmount = 5;
redLedLoop();
analogWrite(redLed, 0);
//
brightness = 0;
fadeAmount = 5;
greenLedLoop();
//
analogWrite(greenLed, 0);
//
brightness = 0;
fadeAmount = 5;
blueLedLoop();
analogWrite(blueLed, 0);
}
}

// the fade routines
//
void redLedLoop() {
for(int i=0; i<255; i++){
analogWrite(redLed, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}

void greenLedLoop(){
//
for(int i=0; i<255; i++){
analogWrite(greenLed, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}

void blueLedLoop(){
for(int i=0; i<255; i++){
analogWrite(blueLed, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
}

Advertisements

MQ2 Gas Sensor and Arduino

 

The MQ2 is one of a series of gas detectors and will detect flammable gasses and smoke. The topic of gas sensors is covered in the Arduino Playground here and you should visit this page for more details. This page also lists the various sensors in the range.

This blog records my efforts with this sensor. The small board I purchased from eBay has four pins, Vcc, Gnd, Analog Output and Digital output. Connect Vcc to 5 volts (do not try to power this from an Arduino Digital Output pin as the current drawn is too great. Connect Gnd to the Arduino Gnd. The sensor outputs a voltage proportional to the concentration of the flammable gas, while the Digital Output switches from High to Low when a certain level is reached. The level at which the DO pin switches is set using a small potentiometer on the board. For the sketch below connect the Analog Output to Arduino Analog Pin A0, and the Digital Output pin to Arduino Pin D2. The sketch will show the value of the voltage output and the state of the Digital Output, on the Serial Monitor (9600 baud).

To test the device I soaked a small piece of cotton wool in a flammable solvent and placed this at the bottom of a glass beaker. After five minutes I placed the sensor into the glass beaker and read the outputs from the Serial Monitor. The board I was using had an LED attached to the Digital Output, that came on when the output was greater than 400 (but this can easily be adjusted).

The sensor will get warm in use and can reach 50 or 60*C. I tested my setup by soaking a small piece of cotton wool in a solvent and placing it at the bottom of a glass beaker. After a few minutes I lowered the sensor into the beaker. I found different solvents gave different voltages, for example Ethanol gave a reading of about 250 to 300, while a low boiling petrol fraction gave a reading in excess of 600.

The manufacturer recommends that the sensor is not exposed to solvent vapour for extended periods.

The following sketch prints out the output voltage and state of the Digital Output, a buzzer could be substituted for the LED attached to the Digital output, but this could become a little annoying after a while.

/* GAS Sensor MQ-2
This sensor detects flammable gasses
the board has four pins
connect AO to Arduino pin A0
connect DO to Arduino pin 2
connect Gnd to Arduino Gnd
connect Vcc to Arduino 5 volts
*/

int sensorPin = A0; // select the input pin for the potentiometer
int DOPin = 2; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
int ledPin =13;

void setup() {
// declare the ledPin as an OUTPUT:
pinMode(DOPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}


void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
Serial.print("Analog Output = ");
Serial.println(sensorValue);
// turn the ledPin on if triggered
//
if (digitalRead(DOPin) ==HIGH){
digitalWrite(ledPin, LOW);
Serial.println("Digital Output = OFF");
}
else {
digitalWrite(ledPin, HIGH);
Serial.println("Digital Output = ON");
}
delay(1000);
}