Week 10: Arduino introduction

I was struggling with the Arduino but fortunately Kyle, one of my classmates, ran a very helpful catch up session to help a few of us get a better hold of the basics.  This  meeting  was  recorded  remotely  on microsoft  teams  which  was  really  helpful  because  it  meant I could  refer back to it and  go through the tutorial at my own  pace.lecture by kyle

Kyle showed us the example bits of code in the Arduino program. At first these were a little daunting because there was a lot of text on the screen. However, I soon realised that the text that came after // was just advice there to help it be easily understood by beginners. We first opened the fade code. This programs the Arduino to turn on an LED and then decrease the brightness at a constant rate till 0. Once it gets to 0 it then gets brighter and brighter up to a maximum value.

notes on fade

Once I got comfortable interpreting the code and figuring out which bits were important and which weren’t, I started to play around changing the values and seeing what they changed. Kyle explained that the fade function could only be done using something called Pulse Width Modulation (PWM). This is where the output is flashed on and off many times a second so that the LED appears dimmer. Only pins 3, 5, 6, 9, 10 and 11 are able to do this so it was important I plugged my circuit into the right ones.

Once I was happy with the fade code, I decided to try and make a working traffic light. Unfortunately I only had one LED to hand but I was able to annotate over the image of the circuit to show where the other two would have been and I was also able to test all three by moving the LED round the circuit to each of the three possible positions. 
Traffic Light circuit
I had the fade and blink code examples open and  used them both as a guide with what to do which I found really useful. At first I got an error message saying that I hadn’t declared what PIN was. By looking at the fade code I figured out that this was because I needed to use ‘int’ to set the names of my pins. I then set them to LED red, amber, and green.
error message traffic light code

Once I had figured out the issue, I was able to successfully program it. I set the Arduino to loop in the sequence green (2000 m/s), amber (flashing 500 m/s on and off), red (5000 m/s). I tested it by putting my LED in each of the slots and it worked perfectly.

I ordered a small electronics kit containing various components including RGB LED’s. I wanted to learn how to control them so I had a look online for a tutorial. I was interested by the LED having 4 pins. makerspace website

https://www.makerspaces.com/15-simple-arduino-uno-breadboard-projects/

The makerspace website had a really good list of 15 beginner projects with useful circuit diagrams and one folder with all the code for the projects in. below is the code for the RGB Led.rgb led circuit

3 of the pins are used for the 3 colours Red Green and Blue. The last pin is the ground. To control the colour, you can mix the amount of voltage down each pin. For example maximum down the red pin and none down the other two will produce red. 

By changing the balance between the pins you can change the colour to pretty much any one you want. To get this range in potential colours, you have to connect the 3 control pins to the PWM pins (3,5,6,9,10 and 11). Once I had installed this code and got it working with the circuit, I then had a play around with the code. I was interested in the logic part of the code (the if brackets). This allows the code to split values between 0 and 767 into 3 catergories.rgb led logic

By playing around with the variable colour (and setting it to a number I chose) I was able to change the colour of the LED.

Print Friendly, PDF & Email