Introduction: Brightness Control PWM Based LED Control Using Push Buttons, Raspberry Pi and Scratch
I was trying to find a way to explain how PWM worked to my students, so I set myself the task of trying to control the brightness of an LED using 2 push buttons - one button increasing the brightness of an LED and the other one dimming it.
To program it, I decided to try to use Scratch. Having done a few projects with my Arduino, I figured this would be a pretty simple project, an LED and two push buttons... how hard could it be right? Boy was I wrong!
It took me two days to get it working, but I learned a lot on the way. I would like to share what I learned with you now
Step 1: Collecting Together the Things You Will Need for This Project
You need a Raspberry Pi and an SD card with Raspian loaded on it. I downloaded the latest version from here and installed it on the micro SD card.
If you are new to Raspberry Pi, there is a nice installation guide on the download page which will show you how to get the operating system set up. I downloaded the Raspian Jessie installation file.
Once the Raspberry Pi is set up, you will need to download and install ScratchGPIO. To do this follow these steps:
Open up a terminal window (at the top of the screen, looks like a small black box)
Make sure you are connected to the Internet then enter these commands:
sudo apt-get update
sudo apt-get upgrade
sudo wget http://bit.ly/1wxrqdp -O isgh7.sh
sudo bash isgh7.sh
Check out the website below as they may bring out a new version for you to install.
Note: Installing ScratchGPIO may no longer be necessary as Raspian Jessie now comes with GPIO control
Now you have done all that we are ready to get cracking on the breadboard setup!
Components you will need for this project:
1 x Breadboard
2 x Push buttons
1 x LED, any colour is fine
1 x 330 ohm resistor ( orange orange brown)
2 x 4.7k ohm resistor (yellow violet red)
4 x small pieces of single core jumper cable ( or you can just use 4 male/male cables if you have them around)
5 x male/female breadboard cables
Make a printout of the GPIO pin guide (trust me you need it, I have it stuck on the wall beside my desk now)
1 x Cup of Tea and a packet of Jaffa Cake Biscuits (Do not miss this step!)
OK, let's make it!
Step 2: Install the LED and Push Buttons on the Breadboard
Orientate the breadboard so the red line is at the bottom.
Push the LED legs into the breadboard taking care to make sure that the negative leg, the ground, is on the left.
Take a pair of thin nosed pliers and straighten the bend in the legs of the buttons, then use a flat surface to get them aligned correctly, with pin 4 and 2 towards the bottom side of the bread board (pin 4 on the left side) gently push the buttons into the breadboard.
Step 3: Ground the LED and Buttons
Orientate the LED so the long leg is on the right hand side
Connect the ground leg of the led to the negative power rail using a 330
Step 4: Insert the Two 4.7k Ohm Resistors Into the Breadboard
Take a 4.7k resistor and push one leg into the positive rail (coloured red) and push the other end of the resistor into the same line as leg 4 of the button (the left leg of the button), repeat this for the other button)
Step 5: Connect the Positive Leg of the LED to the Raspberry PI
Take a male/female cable, push the male end into the same line as the positive leg of the LED.
The female end of the cable will attach to a pin on the Raspberry Pi. Take a look at the GPIO pin out diagram, find the GPIO18 pin (physical pin 12) and connect the cable firmly.
Step 6: Now We Connect the Buttons to the Raspberry Pi
Take a male/female cable and attach the male end to the same line as pin 4 on the button., just above the resistor. Take the female end, and again consult the GPIO pin out diagram. You are looking for GPIO25 (physical pin 22) Connect the cable to this pin.
Now connect another male/female cable to the same line as pin 4 on the second button, and connect the female end of this cable to GPIO9 (physical pin 21) on the Raspberry Pi
Step 7: Adding 5v Power and the Raspberry Pi's Ground to Our Breadboard Circuit
Before applying power to the breadboard, check all your connections carefully or your components and Raspberry Pi could get damaged.
Take a male/female cable and attach the female end to the ground pin located at pin 6 (GND) on the Raspberry Pi. The male end of the cable should be plugged into the ground rail on the breadboard, the blue line at the bottom of the breadboard.
Take another male/female cable and connect the female end to pin two (5v) on the Raspberry Pi, the male end plugs into the red line at the bottom of the breadboard.
The last thing top do is to connect the power rails across the breadboard so that the power and ground rails extend fully across the breadboard. Take a look at the diagram for the small red and black cables at the bottom middle of the breadboard.
Once that's done and you have checked everything carefully once more, power up the Raspberry Pi. Assuming there are no explosions or fires you have finished the physical setup and are ready to get programming with ScratchGPIO!
Step 8: Programming With Scratch!
Here is my program, I hope you have fun making LEDs bright and dim :)
Once you have entered the program you can press z to get the services started, then click the main program, or click the green arrow and get busy pressing buttons :) pressing s will stop everything. Nothing will happen if you don't press z!
Or... if you are feeling super lazy you can just download my program and open it in scratchgpio :) :)