Controlling LED with Keyboard

About This Project
LEDs are like tiny light-bulbs. Learn how to control a LED with your keyboard using Scratch.
Project Info
Programming Platform:
Difficulty:

Introduction

LEDs are like tiny lightbulbs. However, LEDs require a lot less power to light up by comparison.

In this project, we will turn ON LED when the space key is pressed and turn it OFF when released. There is a programmable LED in evive which is connected to the digital pin 13. We will use the same LED for the project.

Components Required

ImageComponentQuantityAvailable in Kit
evive1
USB A-B Cable1

Building Guide

Step 1: What is LED?

Given below are some facts about the LEDs you should know before using one:

  1. Polarity Matters: In electronics, polarity indicates whether a circuit component is symmetric or not. LEDs only allows current to flow in one direction. And when there’s no current-flow, there’s no light.

    The positive side of the LED is called the “anode” and is marked by having a longer “lead,” or leg. The other, negative side of the LED is called the “cathode.” Current flows from the anode to the cathode and never the opposite direction. A reversed LED can keep an entire circuit from operating properly by blocking current flow.
  2. More current equals more brightness: The brightness of an LED is directly dependent on how much current it draws. That means two things. The first being that super bright LEDs drain batteries more quickly, because the extra brightness comes from the extra power being used. The second is that you can control the brightness of an LED by controlling the amount of current through it.
  3. If you connect an LED directly to a current source it will try to dissipate as much power as it’s allowed to draw, and it will destroy itself. That’s why it’s important to limit the amount of current flowing across the LED. Resistors limit the flow of electrons in the circuit and protect the LED from trying to draw too much current.

Step 2: Circuit Diagram

There is no circuit required in this project  😎

You just have to use your evive inbuilt LED.evive Single Colour Programmable LED

Step 3: How to control the LED?

The LED is connected to digital pin 13 internally. So, when the state of digital pin is:

  • HIGH: The LED turns ON, because there is some voltage across its leg, which make current to flow through it.
  • LOW: The LED turns OFF, because there is no voltage difference between the legs. No voltage no current, right.

We know, we can control the digital pin 13 using programming, hence we can control LED.

Step 4: Getting Started with Scratch Programming

Scratch is a graphical programming language which let you interface and control your evive with laptop. If you have not installed Scratch visit here to get full instruction on installing Scratch.

To learn about the Scratch interface, visit here. There are four important things you have to know in Scratch:

  1. Sprites
  2. Stage
  3. Script
  4. Blocks

We have developed these Scratch extensions containing special blocks through which you can control your evive in Scratch:

In Scratch, there are two modes in which you can work:

  • Arduino Mode: Under Arduino mode, the instruction blocks will be tranfer to Arduino C++ into Arduino IDe, and user could modify code in Arduino IDe, and then upload the code into Robot. In that case, the Robot is running offline and it can not interact with Scratch’s stage.
  • Scratch Mode: Under Scratch mode which is defualt mode when mBlock start, mBlock could program robot through USB serial port, the robot is running a program could communicate with mBlock, and robot could intercate with Stage and create more insteresting projects and animations.

In this project we are using keyboard to control, hence we have to work in Scratch mode to use the block which senses the keyboard input.

Step 5: Logic Diagram

We will be continuously checking if the Space key is pressed. This will be either true or false. Now, we will use conditional statements, which will take decision to execute particular statements according to the result. In our case, this is if-else, where if the result is true (i.e. Space key is pressed), LED will be ON, else LED will be OFF.
The next step is to express your logic in a way so that the computer can understand it. This is easy if one uses Scratch since it has built in blocks for basic functions and one does not have to write stuff every time.

Step 6: Scratch Scripts

  1. Whenever you want to run a code, you have to start with a Hat block which is present in Event category. As told above, there is a block named ‘when flag key is pressed’. We will use this block to start our program. Drag the block and drop the block in the script using mouse.
  2. Now, we want to run the code forever. For these type of functions there is a C block named ‘Forever’, which runs the code inside it continuously in loop. Drag the block and snap it below the hat block.
  3. To check the condition drag drop ‘If – Else’ block and place it inside forever block.
  4. To check if the key is pressed or not we will use ‘is key pressed’ from Sensing palette. It gives true when Space key is pressed and false if the Space is not pressed. Place it inside the if else block.
  5. To access the evive digital pin 13, click on Arduino tab, you have to evive via serial port. First connect evive to you PC, using USB B type connect. Select the Board to “Arduino Mega 2560”.

    Click on Connect and select the serial port to which evive is connected.

    Upload or upgrade the firmware to evive.

    Finally in the ‘Extension’ tab select Arduino. Now you are ready to use evive digital and analog pins.

    Once you select a COM Port, you will get a message “Serial port connected” at the top. If you don’t get this message, select another COM port.
  6. If want to turn ON the LED, digital pin 13 should be HIGH, which can be done using ‘set digital pin’ block. Drag and snap the block inside if block. Next to the pin inside the block you can see white space where you have to input the pin number (13 in this case). To turn the pin HIGH, select the option at the end of the block.
    Similarly, turn off the LED in the else block. The final script is given below.

Controlling Pin 13 LED with your Keyboard

Step 7: It's Ready

Your program is ready now and you can press ‘space’ to start it. The whole script will start glowing indicating that the program is running. To stop it press ‘Esc’ on keyboard.

Circuit Diagram

DescriptionCircuit Diagram

only evive is required in this project. We are using Pin 13 LED.

Code

DescriptionDownload Code
Scratch ProgramDownload
Share this Project
Facebook
Twitter
Google+
Pinterest
Reddit
Print
Related Projects
Featured Projects

Leave a Reply

Close Menu
×

Cart