D13DESIGN
  • Home
  • Shop
  • Products
    • Pedal PCBs
    • Shirts
    • Stickers
    • Posters
  • Build Docs
  • Layouts
  • Arduino
21st February, 2021 by Dave

Diorama Lighting 1: Intro

Diorama Lighting 1: Intro
21st February, 2021 by Dave

Introduction and ambitions

We’ve recently renovated our garage into a second living space (a bit more dedicated to the adults of the house) which as well as providing a more dedicated work-from-home space, will also give my wife somewhere nice and comfy to relax and read. The furniture is gradually arriving and next on the shopping list is a bookcase to house her collection of books.

Wanting to make things a little more special, I’m planning a bookshelf diorama (book nook) to sit alongside her Harry Potter books. There’s loads of things I need to learn to get this put together but I’m keen to add some flair with some interesting lighting.

I have some big ambitions for this area of the build and to help me get things done I’ll be using an Arduino to control everything:

  • Day / night cycle
  • Sunset and sunrise effects
  • Shop window lights based on time of day
  • Flickering candles in windows
  • Random thunderstorms at night
  • Random magic spell explosions

I’ll try to document things here in a fairly simple way – hopefully ideal for beginners who’d like to copy/paste some of these ideas into their own projects.

Fingers crossed!

Supplies

I’d like to have full control over the colour of the LED lights I’ll be using so instead of picking single-colour LEDs I’ve picked some RGB LEDs that we can use to choose some specific colour values. I opted for common cathode diffused LEDs to help make some of the future wiring a little easier, and to dull down some of the lights.

Next up is resistors – this is hopefully the only bit of Ohm’s Law we’ll need to cover 🤞

We need to reduce the current that runs through the LEDs in order to stop them burning out. There’s lots of values we can choose from so we need to work out which ones to use. First stop is the data sheet for our chosen LEDs. This tells us that they have a forward voltage (Vf) of between 2v and 2.8v, as well as a forward current (If) of 20mA. Basically, these values define the amount of voltage needed to pass current through the LED and have it light up. The forward current is the maximum safe current we can pass through the LED without it blowing, or getting dangerously hot.

I’ll be using the standard 5v output from my Arduino to power the LEDs so I can use Ohm’s Law (V = IR, or Voltage is equal to current multiplied by resistance) to calculate which resistors I should add to my shopping list. In our case we can calculate the voltage through the LED by minusing the forward voltage from the supply voltage.

R = (Vs - Vf) / If
R = (5v - 2.8v) / 2mA
R = 2.2 / 0.02
R = 110Ω 

Just to double check things I’ll calculate the required resistor using the other possible Vf value for our LEDs:

R = (Vs - Vf) / If
R = (5v - 2v) / 2mA
R = 3 / 0.02
R = 150Ω 

This tells us we should buy resistors of at least 150 ohms – I’m going to play it safe and buy a big bag of 220Ω resistors (1/4w carbon resistors) given that each LED will require 3 of them.

As an example, if we wanted to power our LEDs from an external 12v power supply we’d need a very different resistor:

R = (Vs - Vf) / If
R = (12v - 2v) / 2mA
R = 10 / 0.02
R = 500Ω

Next up we need to pick which Arduino to order. Our RGB LEDs will be driven by passing them a value between 0 and 255 – this lets us specify exactly which colour we want – and we can specify a different value for each leg. To do this, we need to use PWM pins on the Arduino. These pins are enabled for passing this value rather than just a binary on/off (actually HIGH/LOW) value. The problem we’re going to face is that these are often in short supply, in fact the most I could find on a single Arduino board was 15.

If we only want 5 LEDs in our design then this is fine but my ambitions are bigger than that so I need to find a way to increase the number of pins we have available. I can do this by using a 74HC595 Shift Register chip. While this looks daunting it’s actually quite simple. It runs of just 3 PWM pins and gives us 8 new PWM outputs! What’s more, we can daisy-chain these chips together to give us more and more PWM outputs. I’ll get a stack of these as well as some sockets so that when I put things together I’m not directly soldering the legs of the chips.

Final shopping list

  • 1x Arduino Mega 2560
  • 1x 12v 2A power connector for the Arduino
  • 25x common cathode diffused RGB LEDs
  • 100x 220Ω resistors
  • 10x 74HC595 shift registers
  • A decent sized breadboard although 2 of these might be handy
  • Lots of breadboard M-M connectors
  • Some 16-pin IC sockets and some stripboard ready for when I put things together for real

I’ve linked these items to the relevant pages on Bitsbox.co.uk – I don’t get anything from these links but they’re an awesome UK based supplier who I try to use for all my projects.

Testing things out

When plugging in a new Arduino I always expect a few issues so I start simple. I use the Arduino Desktop IDE for my coding and use this to directly send the code to the Arduino. Depending on which one you go for the settings will be a little different but if you opt for the Mega 2560 as I have then you’ll want to go into “Tools > Board” and pick “Arduino Mega or Mega 2560”.

With your Arduino plugged into your computer using USB, you’ll want to go into “Tools > Processor” and select “ATMega 2560 (Mega 2560)” and “Tools > Port” and select “/dev/cu.usbmodem 1411 (Arduino Mega or Mega 2560)”.

Next we’ll write a simple script to test things out – our own variation of “Blink” that will help us know everything’s working. Create a new file and save it somewhere and then we’ll start to code.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

This bit of code runs once when the Arduino fires up and we can use it to set things up. Here we’re setting the mode of the built in LED on the board to be an output. We’ll then be able to pass it data to make it do something. If we had a pin we wanted to capture an input from someone or something (a sensor or a button for example) we’d set that to be ab INPUT pin.

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(1000);
  digitalWrite(LED_BUILTON, LOW);
  delay(1000);
}

Now we’ve added a loop to our code – this will run over and over while the Arduino is powered up. Inside our loop we’re going to send a digital signal to our LED with a value of HIGH. This turns our LED on before a delay of 1000ms (1 second). We then pass a digital signal to our LED with a value of LOW which switched it off before waiting for another 1000ms.

You can now choose “Sketch > Upload” from the main menu and send the code to your Arduino. If it works you’ll see the inbuilt LED come on for a second and then go off for a second. This will loop forever.

To validate things even further I like to change the delay times to something short (100ms) and then upload the file again. You should now see the same behaviour but much more quickly!

RGB LED test #1

Now things are working let’s give an RGB LED a quick test. Given that we’ll be using the shift registers, this code will be through away but it’s exciting and shows some potential of where we can get to. Let’s wire things up like this:

Here we have PWM Pin 11 connected to red, Pin 10 connected to blue and Pin 9 connected green. We can start to set our code up with some variables to track this, as well as a starting value for each pin.

// LED values - starting values between 0 and 255
int redValue = 254;
int greenValue = 1;
int blueValue = 127;

// Direction to cycle - 1 moves the colour forwards, -1 backwards
int redDir = -1;
int greenDir = 1;
int blueDir = -1;

// Pins
const int redPin = 11;
const int greenPin = 10;
const int bluePin = 9;

void setup() {

}

void loop() {

}

Like our Blink example, we need to tell the code that we’re using our LED pins as outputs:

void setup() {
  pinMode(redPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

For our loop we’ll cycle through loads of colours by incrementing the value of each LED pin. When we get to the end of the range we’ll flip the direction and keep going.

void loop() {
  // Set the value of R, G and B through the pins
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);

  // change the values of the LEDs
  redValue = redValue + redDir;
  greenValue = greenValue + greenDir;
  blueValue = blueValue + blueDir;

  // for each color, change direction if
  // you reached 0 or 255
  if (redValue >= 255 || redValue <= 0) {
    redDir = redDir * -1;
  }
  if (greenValue >= 255 || greenValue <= 0) {
    greenDir = greenDir * -1;
  }
  if (blueValue >= 255 || blueValue <= 0) {
    blueDir = blueDir * -1;
  }

  // slight delay so it doesn't rotate color too quickly
  delay(10);
}

Upload that to your Arduino and should hopefully have similar results to this:

Next up

Next I’ll try to get to grips with the shift registers to give me more LED outputs. I’ll also look into how I can trigger light effects in a non-blocking way so that the Arduino can trigger multiple things at the same time – a magic spell during a thunderstorm, for example. This will all start to give me an idea as to how to structure my project code before I start to delve into building the features I’d like in my diorama.

Previous articleFuzztopia 2020Next article Diorama Lighting 2: Shift Registers

About

I love learning new skills and figuring out to make things. Since my day-job is mostly desk based I like to fill my free time with practical activities – here you’ll find some of the projects I like to work on.

Recent Posts

Diorama Lighting 5: Thunder & Lightning23rd February, 2021
Diorama Lighting 4: Second Thoughts & Code Restructuring23rd February, 2021
Diorama Lighting 3: Day & Night22nd February, 2021

Product categories

  • Pedal PCBs (3)
  • Posters (1)
  • Shirts (5)
  • Stickers (0)
  • Uncategorized (0)

Recent Products

  • Pork Chop Fuzz PCB £4.00
  • Green Scream PCB £4.00
  • Continuum Drive PCB £4.00 – £12.00

Recent layouts

Diorama Lighting 5: Thunder & Lightning23rd February, 2021
Diorama Lighting 4: Second Thoughts & Code Restructuring23rd February, 2021
Diorama Lighting 3: Day & Night22nd February, 2021
Diorama Lighting 2: Shift Registers22nd February, 2021
Diorama Lighting 1: Intro21st February, 2021
Rife Wordpress Theme ♥ Proudly built by Apollo13

About This Sidebar

You can quickly hide this sidebar by removing widgets from the Hidden Sidebar Settings.

Latest Products

  • Pork Chop Fuzz PCB £4.00
  • Green Scream PCB £4.00
  • Continuum Drive PCB £4.00 – £12.00
  • 52 Pedal Project Poster £15.20
  • Guitar Collection Tee Alt £15.00

Basket