Assignment 3b: Arduino

Objective: create an electrical circuit that turns on/off a LED
Time: 1 hour
Prerequisites:
  • Arduino Genuino Uno
  • LEDs
  • Other electrical parts (wires, bread board, push buttons, etc.)
  • Software: Arduino IDE

For this tutorial we had to create a circuit that turns on and off a light emitting diode. After it we could play around with more ways to do this.

1. Learning about Arduino

Firstly we learned about Arduino and how it works. We were told that Arduino is a microcontroller that simplifies the making of electrical circuits. It is not like a computer since it does not have an OS (and no file system). Also, it is fast but not super fast and it lacks memory. That is why you have to deal with Arduino in a low level way.

With the Arduino IDE you could easily load software on Arduino and you don’t have to worry about its chip architecture. Low level operations include:

  • Reading a pin’s voltage (high or low)
  • Writing a voltage on the pin

You could read digital (0 or 1), analogue (12 bit sampled) or serial values. For example, for analogue values it holds that the signal is high for 100%, low for 0% and sometimes high/low for 50%.

On Arduino, one chip is not able to have USB input but another one is. This is why addresses are used in the firmware to communicate and send/receive to the other chip.

Lastly, we learned that Arduino’s community is amazing and extremely useful.

2. Set up Arduino IDE

We opened the Arduino IDE with Arduino connected via USB. To make sure that Arduino reads from the right device, two things are done:

  1. Choose the Arduino version that is used at Tools -> Board (in our case: Arduino Genuino/ Uno)
  2. Select the right computer port to read from at Tools -> Port

Then it was time to connect the LED via its two pins. The long pin is plus, the short one minus. Thinking back of old physics classes I connected the plus sign to a Arduino pin (number 13) and the minus sign to the ground pin, to prevent any damage when there is a short circuit.

3. Create code to make the LED blink

Since I had connected the LEDs plus pin to Arduino pin number 13, I could read to and write from that pin in the Arduino’s IDE now. At startup there is already the setup() function (that runs once at the beginning) and the loop() function (that runs with certain interval).

In setup(), we have put the line that tells that we will use pin 13 as an output since we want to put a voltage on the pin to make the LED turn on.

In loop(), we have put the code that will turn the LED on, wait, turn the LED off, and wait again. Thus, the LED will be turned off and on continously with a small break.

void setup() {
  // put your setup code here, to run once:

  pinMode(13, OUTPUT);  // tell that pin 13 is the output here.
}

void loop() {
  // put your main code here, to run repeatedly:

  digitalWrite(13, HIGH); // turn the led on with high voltage level
  delay(1000); // wait one second
  digitalWrite(13, LOW); //now set it low again
  delay(1000); // wait one second
}

The code was then saved, verified and uploaded to Arduino to make it execute at once.

4. Turning the LED on/off with a push button

Adding a button gives interesting extra constraints: whenever the button is pressed, a voltage could flow through it and turn on the LED. When it is not pressed though, it should always be off.

This is solved by connecting one port of the button to a pin, and the other to 5V power source. When the button is pressed, electricity from the power source could flow through the pin and turn on the light. When it is not pressed though, any existing voltage is quickly lead to the ground via a resistor to prevent short circuits. The bread board needed to be used for to be able to use the resistor in this case.

Publicités
Publié dans Uncategorized
Web2PDF