Variation of Project 4

Project 4 of the Arduino Projects Book uses three light sensors (using photoresistors), each tied in code to red, green, and blue. The amount of light on each sensor will control the color of the RGB LED which is the output.

I've struggled a little with this project. For example, I'm not sure exactly what role is played by the Pulse Width Modulation (PWM) outputs. But, anyway, I'm trying to modify this project by replacing the three photoresistors with buttons. Each of the buttons will be programmed to output a different color, i.e. red, green, blue.

And so I've wired the breadboard much the same as Project 4, only instead of photosresistors, I'm using three buttons.

Should work, right? I only have to figure out how to program it. Do I need to use the three PWM outputs (9,10,11) or is there a simpler way?

The PWM outputs are used to vary the brightness of each LED colour, if you're just going to switch them either on or off then you could use a digital output.

Your buttons could be wired better, connect them between ground and a digital input, and use INPUT_PULLUP with the pinMode() function, remembering that logic will be reversed in that a pressed button will be LOW. The way you have them wired will give floating inputs when they're not pressed.

Thank you, Martin,

Obviously, I'm feeling my way through this. It's all new to me.

I thought I would start out with something simpler, a single button that will light up the LED. Here's how it's wired now. I think I don't need to run the wire from 5v, just the ground wire, right? The power is coming from the three PWN outputs.

Have you used the internal pull up as suggested by Martin?

Weedpharma

weedpharma:
Have you used the internal pull up as suggested by Martin?

Weedpharma

I believe I have it wired correctly to be able to use 'internal pullup' in the code. I'll try the one button method before I move on to my three buttons assigned to colors.

Yes, that looks fine for using the internal pull-up. In contrast, some of the button examples in project 1 of the starter kit use pull-down resistors; compare the two methods, I know which one I prefer.

Just to clarify a point, you don't need any external power when using the internal pull-ups, it's all done inside the chip. The voltage required to hold the digital pin HIGH is done by some clever built-in electronic wizardry, you just need a connection to ground when the button is pressed to change it to LOW.

If you get stuck with the sketch there's loads of stuff already on this forum to help you, well worth spending a bit of time searching for it. Good luck with your experiments.