# Difference between revisions of "Tutorials/Arduino Projects/Mobile Robotics/BoeBot/Controlling an LED using a switch"

## Contents

NOTE: This is a place holder for a video.

## Concepts

So you know what a switch is, but what can you do with it?

One of the uses of a switch is to tell the controller to activate/deactivate different components. In this case we are going to use the switch to turn on and off an LED. We will do this by checking to see if the switch is pressed. When it is pressed, we will turn on the LED. when it is released, we will turn off the LED.

## Wiring up the components

To create the circuit you will need to connect a switch as an input and an LED as an output to the Arduino. This means that you will need a switch, an LED, a 10kΩ resistor, a 470Ω resistor, a blue or yellow jumper wire, and a red jumper wire.

parts for the Switch controlled LED circuit

### Connecting the LED

The wiring for the LED is very similar to when we first introduced the LED.

LED schematic

### Connecting the switch

Now you need to connect the switch. First, let us look at the schematic of the circuit to get an idea of what you need to do.

switch schematic

The Arduino pin is connected to the circuit between the resistor and the switch. This is because the digital pin is configured such that it needs a small amount of power to change states. This is useful since we don't want to draw too much power through the Arduino. However, this also means that if the pin is not connected to a known level in the circuit, it could have any value. This is called floating, since the value of the pin is neither high nor low.

To solve this problem we use the resistor to connect the pin to ground. We use a resistor and not just a wire so that we can limit how much current is flowing through the connection. Since we want to minimize the power draw we will use a large resistor, in this case 10kΩ. When the switch is not pressed the pin is only connected to ground through the resistor. Since the pin will have what ever voltage value is applied to it, the resistor can be considered as a wire when the switch is not pressed. However, once the switch is pressed, the pin is directly connected to 5V and the resistor limits the current flowing to just 0.5 mA, and thus prevents a short circuit.

 Equivalent circuit when the switch is open (not pressed) Equivalent circuit when the switch is closed (pressed)

Now that we understand how the circuit works we can start connecting the components.

LED schematic

## Programming

### Configuring ROBOTC

Now that we have everything connected, we need to tell ROBOTC how to configure the pins before we can program. We need to tell ROBOTC that we are using the Parallax BOE Shield, and that we have the LED connected to pin 5. We also need to tell ROBOTC that pin 2 is a Digital High Impedance (the led switch) and name it "ledSwitch".

configuring the pin for the switch controlled LED

Once that is done you should have the following at the top of your source code file.

 ```#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, dgtl2, ledSwitch, sensorDigitalHighImpedance) #pragma config(Sensor, dgtl5, led, sensorDigitalOut) //*!!Code automatically generated by 'ROBOTC' configuration wizard  !!*//```

### Writing the Code

Now that ROBOTC knows what goes where, we can work on writing the code.

We want the Arduino to turn the LED on when the button is pressed, and turn the LED off when it is not pressed. To do this we will need an indefinite while loop, and a way to decide what to do based on the status of the switch. There are lots of different ways to do this, but for now, we will just use a second while loop that only runs when the button is pressed.

 ```#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, dgtl2, ledSwitch, sensorDigitalHighImpedance) #pragma config(Sensor, dgtl5, led, sensorDigitalOut) //*!!Code automatically generated by 'ROBOTC' configuration wizard  !!*//   task main() { while(true) { while(the switch is pressed) //if the switch is pressed run the code in the loop { //the switch is pressed so turn the LED on. SensorValue[led] = 1; } //the switch is not pressed so turn the LED off SensorValue[led] = 0; } }```

We already know how to turn LEDs on and off, but how do we check the state of the button? Well, we use the following command.

 `SensorValue[ledSwitch] == 1`

The "==" means to check if the values to either side are equal. If the values are equal, then it will return true, otherwise it will return false. So by placing that code inside the second while loop's condition, we can make that loop only run when the switch is pressed.

 ```#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, dgtl2, ledSwitch, sensorDigitalHighImpedance) #pragma config(Sensor, dgtl5, led, sensorDigitalOut) //*!!Code automatically generated by 'ROBOTC' configuration wizard  !!*//   task main() { while(true) { while(SensorValue[ledSwitch] == 1) //if the switch is pressed run the code in the loop { //the switch is pressed so turn the LED on. SensorValue[led] = 1; } //the switch is not pressed so turn the LED off SensorValue[led] = 0; } }```