What is an LED & How to Use It with evive?

Description
Learn about LEDs (light emitting diodes), how to interface them with evive, and how to program them in PictoBlox. Explore how to control the brightness of the LED using PWM and make it blink in both real-time and upload mode.

Introduction

LEDs (short for light emitting diodes) are fascinating little things. Though tiny, together they are capable of lighting up an entire room! Learn what is LED, how do LEDs work, how to interface them with evive and program them in PictoBlox – our Scratch blocks-based graphical programming platform with advanced hardware interaction abilities, and finally what exciting DIY projects you can make using the colorful light emitting diodes available in the evive Starter Kit.

To work in PictoBlox, you’ll first need to download it from HERE:

Ready? Set. Go!

What is LED (Light Emitting Diode) and How do LED works?

Light emitting diodes, aka LEDs, are tiny light-bulb-like devices that glow when current passes through them.

Basic properties of an LED

Let’s have a look at three important concepts we must keep in mind when dealing with LEDs:

  1. Polarity: Just like batteries, LEDs also have positive and negative terminals. Therefore, they allow the current to pass ONLY in one direction, i.e. from the positive terminal to the negative terminal. Connecting them in reverse might blow them up. Out of the two, the longer one is the positive terminal, and the other is negative. However, this isn’t a foolproof method. If you look closely at the base of the LED, you’ll notice that the boundary is flat/cut above one of the terminals. This terminal is the negative terminal. The other is, therefore, positive.Polarity of LED
  2. Intensity: We know that according to Ohm’s law, currents increase when the voltage across the circuit is increased. This law can be exploited to vary the brightness of the LED, by changing the amount of current passing through it. As the current increases, the brightness also increases, and vice-versa.
  3. Current Limit/Rating: Their current rating is the maximum value of the current they can withstand; if you pass a current greater than that value, they may blow up! The safe value of current for LEDs is usually around 20 milliamperes. To limit the value of current we use resistors, the value of which is calculated using Ohm’s law.

How to interface an LED with evive

Now that we have a little idea about LEDs, let’s take a look at how to interface it with evive and see it in action.

evive already has an LED connected to Digital Pin 13. evive Pin 13 LED

If you want to connect an external LED, you can make a circuit like this on evive’s breadboard and connect the +ve terminal of the LED to a digital pin through which you want to control the LED.connect LED with evive

Making the LED Blink in Real-Time

Now, we have an LED connected to the evive, let’s write a script to make the LED connected to Pin 13 blink in real-time using PictoBlox- a graphical programming software based on Scratch blocks. Follow the steps to write the script to make the LED blink after each second.

  1. Open PictoBlox. Connect evive/your prototyping board to the computer using the USB cable.
  2. Click on the board button and select evive from the drop-down.
  3. Next, from the dialogue box that appears, select the appropriate serial port.
  4. We will use the set digital pin () output as () block to turn the LED ON/OFF. Choose the pin to which the LED is connected in the first drop-down menu and the second drop-down menu helps to set the status of the LED whether ON/OFF.set digital pin block
  5. Drag and drop the set digital pin block. By default Pin 13 is already selected, now to on the LED, select HIGH from the second dropdown.
  6. Add a wait of 1 second below the set digital pin.Real Time Making LED blink 1
  7. Now, to turn the LED off, duplicate the above set of codes and drop it below the wait block.
  8. Now, into the second drop-down of the second set digital pin select LOW. Real Time Making LED blink 1
  9. To blink the LED continuously, use the forever block around the entire code.
  10. Now, place a when flag clicked hat block above the forever block. It will ensure that the script runs when the green flag is clicked.Making the LED Blink

You can download the entire code to make the LED blink in real time here.led blink real-time mode

Controlling Brightness of pin 13 LED Using PWM in Real-Time

To control the brightness we will use Pulse Width Modulation. It is a technique to reduce the average voltage at a digital pin by controlling the width of the digital pulse at that pin.

What is PWM (Pulse Width Modulation)?

PWM or Pulse Width Modulation is a technique for getting an analog output using digital signals. A digital signal, in general, can have either of two values: HIGH/ON or LOW/OFF. If we switch the signal between these two values at an extremely fast rate, say, 500 times in 1 second, the signal at the output will appear to be continuous; it will seem as if it is an analog signal.

evive generates analog output in PWM form. It has 12 PWM pins for the same; they are numbered 2 – 13. Pin 13 is internally connected to the pin 13 LED.

Script to Control the Brightness in Real-Time

Follow the steps given here, to control the brightness of the LED in real time.

  1. Here, we want the brightness of the LED to change to the value we enter in PictoBlox in real-time.
  2. Now, go to the Variables palette and create a variable named Brightness. Once you create it, you’ll also see it in the top left corner of the stage. Right-click on the variable in the stage. From the drop-down menu, select slider. In doing this you’ll see a slider, i.e. a line and a small rectangle that you can slide on that line. You can control the brightness of the LED with it.Slider Variable
  3. Next, use to the set PWM pin () output as () block to set the brightness of the LED.set pwn pin
  4. We, need to set the brightness of the LED by the value entered by us in the brightness variable, thus drag and drop brightness variable into the blank space given. And select the Pin to which the LED is connected in the first drop-down.Real Time Changing Brightness of LED
  5. Add a wait of 0.1 seconds to gradually change the brightness.
  6. Add a forever block around the script to run it forever.
  7. Finally, add when flag clicked hat block to execute the program.Controlling Brightness of LED

Click on the green flag to run the script, and then slide the ball on the slider to change the brightness of the LED.

You can find the entire script to change the brightness of the LED using PWM here.LED Brightness

Working with the Light Emitting Diode in Upload Mode

evive Notes Icon
When working with evive on PictoBlox for the very first time, or if you’ve previously uploaded another code to evive, you must upload the firmware in order to upload new code to it. You can find the procedure for the same HERE.

Follow the steps below to write the script:

  1. To work in the upload mode, click the toggle button.
  2. We will use the set digital pin () output as () block to turn the LED ON/OFF. Choose the pin to which it is connected in the first drop-down menu and the second drop-down menu helps to set the status of it whether ON/OFF.set digital pin block
  3. Drag and drop the set digital pin block. By default Pin 13 is already selected, now to turn it on the LED, select HIGH from the second dropdown.
  4. Add a wait of 1 second below the set digital pin.Real Time Making LED blink 1
  5. Now, to turn the LED off, duplicate the above set of codes and drop it below the wait block.
  6. Now, into the second drop-down of the second set digital pin select LOW. Real Time Making LED blink 1
  7. To blink the LED continuously, use the forever block around the entire code.
  8. As we are going to upload the script to evive, we will use the when evive starts up hat block above the entire script to execute the program.
  9. Now, upload the code using the upload button.

Below is the complete script:Making the LED Blink in Upload Mode

You can download the code to make the LED blink in upload mode from here.LED Blink in Upload Mode

Conclusion

In this lesson, we learned about LEDs, their basic properties, how to interface them with evive, and how to program them using PictoBlox. We also experimented with varying the brightness of the LED using PWM and made it blink in both real-time and upload mode. Hopefully, you have now gained some knowledge on using LEDs with evive. Now, you can use LEDs to create amazing DIY projects!

Table of Contents