Introduction: RGB Distance Dependant Light

You are probably thinking: "RGB Distance dependant light", what is that? Let me explain the idea to you. For a school project I was required to make a smart object, so I decided to combine a lamp with an ultrasonic sensor. The entire project is powered by an arduino UNO R3.

The machines and tools you will need:

  • Laser cutter (minimal size 290x174 mm)
  • A drill with a 5mm bit (used to drill out the holes to fit the LED's)
  • Possibly a exacto knife (might come in handy if your cut pieces that haven't been cut all the way through)
  • Screwdriver according to your screw heads
  • Optionally a hecksaw

The products you will need:

  • Arduino UNO R3
  • 3mm Plywood
  • 3x rgb led
  • 3x 220 ohm resistor
  • 1x ultrasonic ping sensor (The model I used was a HC-SR04)
  • 4x M4 nuts and bolts

Step 1: Making the Casing

The casing is designed to snugly fit all the components I used. To make this casing i used 3mm plywood and cut the panels to size using a laser cutter. The slot you see in the top panel should have been used to insert a acrilic panel, but due to time and difficulty constraints I unfortunately wasn't able to finish that part.

The model is designed to be used with a external power adapter for the arduino. The front two holes are used for the "eyes" of the ultrasonic sensor. I designed them to be a snug fit.

The minimal size of the laser cutter you'll need to print out the included case as is, would be 290 x 174 mm

For the led's there are three long sided pieces on the bottom right. These are designed to snugly fit internally as a platform for the led's to rest in. When you hav cut the panels and removed them form the wood, you sould drill two additional holes in the bottom of the tray. This hole should be snug to fit the led's in. In my case this was solved by using a 5 mm drill bit.

Step 2: Making the Electronics

For the electronics i used 2 RGB LED's with 3 220 ohm resistors. The led's are soldered in a parallel setup as shown in the included diagram. I chose to solder wires directly to the header of the ultrasonic sensor. To attach wires in a arduino I used some male headers and soldered all the leads to the correct terminals: The pins area soldered as followed; The trig is soldered to pin 13, the echo to pin 12, the red annode to 11, the green annode to 10 and the blue annode to 9.

HC-Sr04 Datasheet: http://www.electroschematics.com/8902/hc-sr04-datasheet/

Step 3: Progamming

The way I went about programming this device was quite a journey. I wanted the led's to react on the ultrasonic sensor and change color according to the distance the ultrasonic sensor returned. When the distance between object and sensor is low, so the object is close, the color is red. The further away the object goes, the greener the color gets. Evrithing in between is a mix of those colors and scales up as the distance increases. This results in a color spectrum between red and green.

Upload the code to the arduino cause you wont have acces to the usb port once the arduino is inserted in the casing.

/*
RGB cycle using an ultrasonic sensor.

Author: Bryan Vermaat - 2016

See https://www.instructables.com/id/RGB-Distance-Depen... for a usage of this code.

This code is publicly available and may be edited/altered *

/ The three primary colour LEDs, driven as analgue outputs (actually PWM, but // close enough for our analogue eyes).

const int ledPinRed = 11; // Red LED connected to analogue out pin const int ledPinGrn = 10; // Green LED connected to analogue out pin const int ledPinBlu = 9; // Blue LED connected to analogue out pin

const int trigPin = 13; const int echoPin = 12; //const int modeSwitchPin = 12;

// The Hue potentiometer goes on an analogue pin, taking the pin from // 0V to 5V.

const int potPinHue = 0;

// Constants to define the ranges.

const int hueRedLow = 0; const int hueRedHigh = 255; const int hueBlue = 170;

// The minimal and maximal range for the ultrasonic sensor.

const int rangeMin = 0; const int rangeMax = 200;

const int brightMin = 0; const int brightMax = 255;

// Work variables.

// The potentiometer value is mapped to the range 0 to 360 (degrees). int valueHue;

// The hue is the range 0 (red) to 170 (blue) in rainbow // mode or 255 (red) in colour wheel mode. // The brightness ranges from 0 (dark) to 255 (full brightness)

int hue, brightness;

// The saturation is fixed at 255 (full) to remove blead-through of different // colours. // It could be linked to another potentiometer if a demonstration of hue // is desired.

const int saturation = 255;

// The brightess of each LED (0 to 255).

unsigned int r, g, b;

void setup() { // Still need to set a baud rate, even for USB. Serial.begin(9600);

// Set LED pins to output. pinMode(ledPinRed, OUTPUT); pinMode(ledPinGrn, OUTPUT); pinMode(ledPinBlu, OUTPUT);

// Set the pins for the ultrasonic sensor. pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); }

void loop() { // The Hue potentiometer value is mapped to degrees - 0 to 360 - for convenience. valueHue = map(MeasureDistance(), 0, 1023, 0, rangeMax);

// Colour wheel mode (red to red, wrapped around in a cycle).

hue = map(valueHue, rangeMin, rangeMax, hueRedLow, hueRedHigh);

// The brightness is fixed at full for the colour wheel. This could be // linked to another poteniometer if that is a concept you wish to // demonstrate. brightness = 255;

// Do the conversion. HSBToRGB(hue, saturation, brightness, &r, &g, &b);

analogWrite(ledPinRed, r); analogWrite(ledPinGrn, g); analogWrite(ledPinBlu, b);

Serial.print(" bright="); Serial.print(brightness); Serial.print(" hue="); Serial.print(hue); Serial.print(" red="); Serial.print(r); Serial.print(" green="); Serial.print(g); Serial.print(" blue="); Serial.print(b); Serial.println(""); delay(50); }

// This function taken from here: // http://eduardofv.com/2011/01/15/arduino-rgb-led-h...

void HSBToRGB( unsigned int inHue, unsigned int inSaturation, unsigned int inBrightness, unsigned int *oR, unsigned int *oG, unsigned int *oB ) { if (inSaturation == 0) { // achromatic (grey) *oR = *oG = *oB = inBrightness; } else { unsigned int scaledHue = (inHue * 6); unsigned int sector = scaledHue >> 8; // sector 0 to 5 around the color wheel unsigned int offsetInSector = scaledHue - (sector << 8); // position within the sector unsigned int p = (inBrightness * ( 255 - inSaturation )) >> 8; unsigned int q = (inBrightness * ( 255 - ((inSaturation * offsetInSector) >> 8) )) >> 8; unsigned int t = (inBrightness * ( 255 - ((inSaturation * ( 255 - offsetInSector )) >> 8) )) >> 8;

switch( sector ) { case 0: *oR = inBrightness; *oG = t; *oB = p; break; case 1: *oR = q; *oG = inBrightness; *oB = p; break; case 2: *oR = p; *oG = inBrightness; *oB = t; break; case 3: *oR = p; *oG = q; *oB = inBrightness; break; case 4: *oR = t; *oG = p; *oB = inBrightness; break; default: // case 5: *oR = inBrightness; *oG = p; *oB = q; break; } } }

// Function to measure the distance using an HC-SR04 ultrasonic sensor long MeasureDistance(){ long duration, distance; digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if (distance >= rangeMax || distance <= rangeMin){ Serial.println(" Out of set range. returning Max."); return rangeMax; } else { Serial.print(distance); Serial.println(" cm"); return distance; } }

Step 4: Assembly

You sould combine and glue the two pieces of the casing seperately, there area t shaped holes that have to be faced upwards so the bolts an nuts can be inserted later. The top is now ready for the led insertion. Put the led's in the therefore drilled holes. Up next i started the total assembly by putting the power lead through the hole and connecting it to the arduino, caus this would be a lot harder to do later in the process. Up next I lowered the arduino in place and slided the ultrasonic sensor in its designated place. Then it was time to close the lid, cerfully put the lid on and secure it using the nuts and bolts on the top side.

Then it's just a matter of connecting the power plug to the wall outlet and voila, your creation should be lighting up and according to the distance sould be changing color.