1. An LED as a Digital Output Device In this lesson, we will learn how RoboBlockly can control an LED (light emitting diode) that is attached to an Arduino microcontroller board. The Arduino and LED will be virtual simulations, displayed on the RoboBlockly grid. An LED is a digital output device, meaning that we can output (send) a signal to the device to do something. Each digital output device has two possible states, HIGH and LOW, usually corresponding to whether the device is on (HIGH) or off (LOW). For a real Arduino the HIGH and LOW states are represented by voltages. The Arduino uses 5 volts to represent HIGH and 0 volts to represent LOW. So to turn on an LED, we instruct the Arduino to send a "HIGH" (or 5V) signal to it. To turn it off, the Arduino sends a "LOW" (or 0V) signal to the LED.
2. Specify the Arduino Pin using the pinMode block To have an Arduino output voltage (i.e., send the voltage signals) to an LED, we need to know which Arduino pin the LED is connected to, and then we need to set up that pin for output. We do this using the function pinMode( PIN#, MODE), where MODE is either OUTPUT or INPUT and PIN# is the number of the pin being used. For a real Arduino, we use the pinMode code block that is found in the Arduino blocks menu. The example below will set pin 13 to OUTPUT mode: We will use this pinMode block in Unit 2. But for our virtual simulation of an Arduino with LED, we use the following virtualPinMode code block: This virtualPinMode code block can be found in the Functions blocks menu. (Note that it is only available in the lessons and activities for this Arduino with LED virtual simulation.) As with the regular pinMode block, you specify the number of the pin and whether the mode is INPUT or OUTPUT. (The OUTPUT/INPUT code block is available in the Arduino blocks menu.)
3. Send the Signal to Turn the LED On or Off Once the pinMode is set (and assuming that an LED is connected properly to that pin), we can instruct the Arduino to turn on the LED by sending a HIGH (5V) signal to that pin, or turn it off by sending a LOW (0V) signal to the pin. To do this, we "write" the signal to the pin by using the function digitalWrite(PIN#, STATE), where STATE is either HIGH or LOW. For a real Arduino, we use the digitalWrite code block that is found in the Arduino blocks menu. The example below will send a HIGH output signal to pin 13: As with the regular pinMode block, we will use this digitalWrite block in Unit 2. But for our virtual simulation of an Arduino with LED, we use the following virtualWrite code block: This virtualWrite code block can be found in the Functions blocks menu. (Like the virtualPinMode block, it is only available in the lessons and activities for this Arduino with LED virtual simulation. But the HIGH/LOW code block is available in the Arduino blocks menu.)
4. RGB LED Module Connected to Arduino The image in the RoboBlockly grid below shows how the Barobo RGB LED module is connected to an Arduino for this activity. (You can enlarge the grid image using the Grid Size S, M, or L options below the grid.) In particular, the LED module is connected to pins 11, 12, 13, and GND on the Arduino, so that the pin labeled "R" on the RGB LED module (the red LED) is plugged into pin 13 on the Arduino, the "G" pin (green LED) is in pin 12, the "B" pin (blue LED) is in pin 11, and the "-" pin is in the GND, or ground, pin. We can then turn on one of the LED colors by setting the corresponding pin mode to OUTPUT and then sending a HIGH value to that pin. Or if it's on, we can turn it off by sending a LOW value. Example 1 shows how it is done for the red LED.
5. Virtual Code Blocks Defined as Functions Note that the virtualPinMode and virtualWrite code blocks are defined as functions. If you scroll down in the Workspace you can see their code. It's a good exercise to study it to see how it works, but don't change it. (Or, if you want to experiment with it, save a copy of the code for your own use.) Click the More Info button at top right for more information on creating and using functions in RoboBlockly.
|
Use the virtual Arduino code blocks to turn on the red LED, wait two seconds, then turn it off. |
Grid Lines: | ||
Tics Lines: |
|
Width
px
|
Hash Lines: |
|
Width
px
|
Labels: |
|
Font
px
|
Trace Lines: | ||
Robot 1: |
|
Width
px
|
Robot 2: |
|
Width
px
|
Robot 3: |
|
Width
px
|
Robot 4: |
|
Width
px
|
Grid Size: S M L | Simple View: | MinView: |
![]() |
|
Coord: x-axis y-axis | Grid Lines: x-axis y-axis | |||
Grid: | 12x12 inches | 24x24 inches | 36x36 inches | |
72x72 inches | 96x96 inches | 192x192 inches | ||
1x1 inches | 2x2 inches | Fraction: | ||
xy-Range: | ||||
|
||||
|
||||
Quad: | 1 Quadrant | 4 Quadrants | 1&4 Quadrants | |
Units: | US Customary | Metric | Ruler: | |
Label: |
|
|
|
|
|
|
|
|
|
|
|
|||||||||||
|
|||||||||||