College of Engineering, Architecture and Fine Arts Laboratory Activity 1: Quick Start in Arduino Microcontroller: Interfacing LEDs, and Designing of Simple Traffic and Car Tail Light Systems Objectives: To get the Arduino set-up up and running, learn, and execute some interesting projects using microcontroller and LEDs including designing a simple traffic lights system. Introduction: Having assumed that we have an up and running Arduino hardware and successfully installed the software, we can now start on our first exciting project. It’s not that exciting, but we need to start somewhere, and this will ensure that we have everything set up correctly to use our Arduino board. We are going to modify the example Blink sketch that comes with Arduino – this is the Hello World of a microcontroller. We will increase the frequency of the blinking and then install the modified sketch on our Arduino board. Rather than blink slowly, our board will flash its LED quickly. We will then take the project a stage further by using a bigger external LED and resistor rather than the tiny built-in LED, and finally, develop a very common microcontroller activity which is known as the running/chasing LED. Lab Procedures: First, we need to load the Blink sketch into the Arduino software. The Blink sketch is included as an example when you install the Arduino environment. So, we can load it using the File menu, as shown in Figure 1 below: Figure 1. Example Code: Blink Most of the text in this sketch is in the form of comments. Comments are not actually part of the program but explain what is going on in the program to anyone reading the sketch. Comments can be single-line comments that start after a // and continue to the end of the line, or they can be multiline comments that start with a /* and end some lines later with a */. If all the comments in a sketch were to be removed, it would still work in the same way, but we use comments because they are useful to anyone reading the sketch trying to work out what it does. Before we start, a little word about vocabulary is required. The Arduino community uses the word “sketch” in place of “program,” so from now on, we will refer to our Arduino programs as sketches. Occasionally we may refer to “code.” Code is programmer speak for a section of a program or even as a generic term for what is written when creating a program. So, someone might say, “I wrote a program to do that,” or they could say, “I wrote some code to do that.” To modify the rate at which the LED will blink, we need to change the value of the delay so that in the two places in the sketch where we have: delay(1000); change the value in the parentheses to 200 so that it appears as: delay(200); Microprocessors and Microcontroller Systems and Design Darwin D. Mañaga, Meng-ECE, PECE, ACPE Leading Innovations, Transforming Lives This is changing the delay between turning the LED on and off from 1000 milliseconds (1 second) to 200 milliseconds (1/5th of a second). In the succeeding projects, we will explore this sketch further, but for now, we will just change the delay and download the sketch to the Arduino board. With the board connected to your computer, click the Upload button on the Arduino. This is shown in Figure 2. Figure 2. Uploading a Sketch to the Arduino If everything is okay, there will be a short pause and then the two red LEDs on the board will start flashing away furiously as the sketch is uploaded onto the board. This should take around 5 to 10 seconds. If this does not happen, check the serial port, and board type settings as described in the previous sections. When the completed sketch has been installed, the board will automatically reset, and if everything has worked, you will see the LED for digital port 13 start to flash much more quickly than before. Now, this doesn’t really seem like real electronics because the hardware is all contained on the Arduino board. In this section, we will add an external LED to the board. LEDs cannot simply have voltage applied to them; they must have a current-limiting resistor attached. The Arduino board connectors are designed to attach “shield” plug-in boards. However, for experimentation purposes, they also allow wires or component leads to be inserted directly into the sockets. Figure 3 shows the schematic diagram for attaching the external LED. This kind of schematic diagram uses special symbols to represent the electronic components. Figure 3. Connecting an External LED The LED appears rather like an arrow, which indicates that light-emitting diodes, in common with all diodes, only allow the current to flow in one direction. The little arrows next to the LED symbol indicate that it emits light. The resistor is just depicted as a rectangle. Resistors are also often shown as a zigzag line. The rest of the lines on the diagram represent electrical connections between the components. These connections may be lengths of wire or tracks on a circuit board. In this case, they will just be the wires of the components. We can connect the components directly to the Arduino sockets between the digital pin 12 and the GND pin, but first we need to connect one lead of the LED to one lead of the resistor. It does not matter which lead of the resistor is connected to the LED; however, the LED must be connected the correct way. The LED will have one lead slightly longer than the other, and it is the longer lead that must be connected to digital pin 12 and the shorter lead that should be connected to the resistor. LEDs and some other components have the convention of making the positive lead longer than the negative one. Connect the LED’s long lead into the digital pin 12 and the free lead of the resistor into one of the two GND sockets. We can now modify our sketch to use the external LED that we have just connected. All we need to do is change the sketch so that it uses digital pin 12 instead of 13 for the LED. To do this, we change the line: int ledPin = 13; // LED connected to digital pin 13 Microprocessors and Microcontroller Systems and Design Darwin D. Mañaga, Meng-ECE, PECE, ACPE Leading Innovations, Transforming Lives to read: int ledPin = 12; // LED connected to digital pin 12 Now upload the sketch by clicking the Upload to the Board button in the same way as you did when modifying the flash rate. We have created our first project, albeit a very simple one. In the next chapter we will get a bit more background on the Arduino before moving on to some more interesting projects. Now, ever seen dancing LEDs? Ever wondered you can make your own dancing LED for your home decoration. So, as a challenge, develop a sketch and execute a running lights project. You will need to use 8 LEDs with the light chasing right, then, chasing left. Design and Implementation of a Simple Traffic Light System: Before proceeding with the design, explore the implementation using the following Arduino programming principles: Digital I/O Time Digital Pins digitalRead() digitalWrite() pinMode() delay() delayMicroseconds() micros() millis() Communication Serial.begin() In this project you’re going to build a traffic lights system: ▪ There are 3 LEDs with different colors (green, yellow, and red) to emulate the traffic lights for the cars ▪ There are 2 LEDs with different colors (green and red) to emulate the traffic lights for the pedestrians ▪ There is a pushbutton to mimic the ones in the pedestrians traffic lights Components: ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ 1x Breadboard Arduino Microcontroller Board 3x 5mm LED (1x red, 1x yellow, 1x green) 2x 3mm LED (1x red, 1x green) 5x 220Ohm Resistor 1x 10kOhm Resistor 1x pushbutton Jumper Wires Note: If you don’t have LEDs of different sizes, it is ok. The project should still work. Project Requirements: ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ ▪ Here’s some tips to better understand what’s going on. The car light is always green, and so the pedestrian light is always red unless someone presses the button. When someone presses the button here’s what happens: The car light changes to yellow and then to red The pedestrian light changes to green The lights are in this state for a while (in the code this time is the variable crossTime) The pedestrian green light flashes and goes to red The car light changes from red to green The pedestrian light changes to green Microprocessors and Microcontroller Systems and Design Darwin D. Mañaga, Meng-ECE, PECE, ACPE Leading Innovations, Transforming Lives Design and Implementation of a Car’s Tail Light System: Using your Arduino Hardware or Tinkerkad, create a sketch that will execute the functionality of an automobile’s rear turn signal light, i.e.: 1. Imagine there are 4 LEDs for each rear light signal indicator of an automobile, as illustrated below: 2. The driver has the options to do either the following, but not simultaneously: a. No operation: Both indicators are idle b. Enable the right signal indicator: Once the trigger for the Right Signal Indicator has been armed, light scrolls from LED5 to LED8 with time interval of 300msec per LED transition, i .e.: i. LED5 turns on, ii. after 300msec, LED6 turns on, iii. after 300msec, LED7 turns on, iv. after 300msec, LED8 turns on, v. all LEDs are then disabled, vi. Continuously execute the sequence from step i. a. Enable the left signal indicator: Once the trigger for the Left Signal Indicator has been armed, light scrolls from LED1 to LED4 with time interval of 300msec per LED transition, i.e.: i. LED1 turns on, ii. after 300msec, LED2 turns on, iii. after 300msec, LED3 turns on, iv. after 300msec, LED4 turns on, v. all LEDs are then disabled, vi. Continuously execute the sequence from step i. b. Enable the emergency hazard: All LEDs are flashing at a rate of 500msec. c. Include a flag signal (another LED) which would correspond to any invalid simultaneous operations. i.e.: Both right and left signal indicators are triggered. Note: Turn in your pdf lab report which includes the following, together with a video clip showing the functionalities of each given task. Make sure to provide a detailed explanation as you present your work. References: 30 Arduino Projects for the Evil Genius by Simon Microprocessors and Microcontroller Systems and Design Darwin D. Mañaga, Meng-ECE, PECE, ACPE Leading Innovations, Transforming Lives