Wednesday, July 12, 2017

8 - Arduino: decoding TV remotes infrared signals with an IR receiver


- Layout for this exercise:

- The TV remotes used in this exercise are of very common brands, like Samsung and Sony:

1 - Introduction

- Infrared (IR) radiation has slightly longer wavelength and shorter frequency than visible light, just next to the red color, but it is undetectable to the human eye.

- IR radiation can be used as a wireless communication technology. 

- IR is used in so many environments, for instance for turning on/off TV with a remote, and for selecting different options like channels, brightness, volume, etc ...

- A TV remote uses an IR transmitter that sends information to an IR receiver on the TV device, with usually a modulated frequency of 38 KHz. 

- The goal of this exercise is double: on the one hand to decode the IR signals from two different TV remotes, on the other hand to use the TV remotes to make colored LEDs to blink, taking advantage of the hexadecimal values of the decoded IR signals:

- About the schematics used in this exercise, the IR sensor has got three legs connected in this way:

  • VCC = 5 V
  • GND = GND
  • Data = pin 11

2 - Decoding TV remotes with an IR receiver

- Before using the Arduino with an IR sensor the IRremote library must be installed:

- The Arduino sketch for decoding the TV remote signals:

- Once the TV remotes are pointed to the IR sensor and pressed some buttons the serial monitor outputs the hexadecimal values of the decoded signals:

- Actually, in this exercise just the two first values will be used, corresponding to the "power" buttons of the Samsung and Sony remotes:

  • 0xE0E040BF
  • 0xA90 (three times)

- Testing the decoding process:

- Let's analyze the code of the Arduino sketch.

- First of all, the IRremote library is imported:

- The pin 11 is assigned to the receiving and the decoding process, because it is connected to the data leg of the IR sensor, and a receiver object is created:

- The serial monitor is set to 9600 bauds, and the receiving process is started:

- If a code is received True is returned, the value is stored in the variable "results", and the serial monitor prints that value. Finally, irrecv.resume() resets the receiver and prepares it to detect another code:

3 - Blinking LEDs with remotes

- Now, two LEDs are handled with the TV remotes, using the Sony remote to turn on the yellow LED and the Samsung remote to turn on the red LED.

 - Code for the Arduino sketch:

- Let's analyze the code.

- First of all, the values achieved at the previous exercise are declared:

- The pins are assigned and a receiver object is created:

- The 
serial monitor is set to 9600 bauds, the receiving process is started, and also the LED pins are set to output mode:

- As explained before, if a code is received True is returned, the value is stored in the variable "results", and the serial monitor prints that value. Finally, irrecv.resume() resets the receiver and prepares it to detect another code:

- Now, depending on the value of the received code, either matching 0xE0E040BF or 0xA90, the yellow or red LEDs start blinking every half second:

- Testing the circuit, when the Samsung remote's "power" button is pressed the yellow LED blinks, and when the Sony remote's "power" button is pressed the red LED blinks:

Tuesday, July 11, 2017

7 - Arduino: strobe light effect


- Layout for this exercise:

1 - Introduction

- The objective of this exercise is to achieve the light effect of strobe based on emitting flashing light by 8 red and yellow LEDs, simulating the flag of Spain.

- The strobe light effect is a visual phenomenon caused by aliasing that occurs when continuous motion is represented by a series of short or instantaneous samples. 

- It occurs when the view of a moving object or blinking light is represented by a series of short samples as distinct from a continuous view at a rate close to the sampling rate.

- The schematics of this exercise is an Arduino board connected to 8 LEDs and a potentiometer connected to the analog pin A0. 

2 - Code

- The code for this exercise:

- Let's analyze the code.

- Setting the pins for the couple of LEDs (digital pins 12,11,10 and 9) and the potentiometer (analog pin A0):

- The readings on the A0 are calibrated from 0 to 1023 giving an interval of 10 to 500 ms, what is a time enough short so that the human eye perceives the light as an overlay of flashes leading to the strobe effect.

- As seen in the previous section the function repeater is applied to the digital pins 12, 11, 10 and  9:

3 - Testing the circuit

- The video shows how the blinking light speed is controlled by the potentiometer in the interval of 10 to 500 ms, simulating the flashing colors of the Spanish flag:

Monday, July 10, 2017

6 - Arduino: Relay


- Layout for this exercise:

1 - Introduction

A relay is an electromechanical switch that can control much more voltage and current than an Arduino pin.

- For instance, a relay could be useful to connect an Arduino to a device like a lamp or any other electrical machine of 120 V.

- A relay consists of a coil of wire and switch contacts. When power is applied to the coil it becomes magnetized and pulls the switch contacts closed. 

 A transistor is used to handle the current because in this case much more power is needed than an Arduino can provide.

-  There are 3 contact pins used to connect the device handled by the relay:

  • COM = common            <- connected to power
  • NC = Normally Closed  <- connected to the yellow LED
  • NO = Normally Open    <- connected to the red LED

- Also, there are 2 more pins for the coil used to control the relay. One side is connected to the collector of the transistor and the other one to 5 V.

- The relay can be in 2 different modes:

  • OFF = COM is connected to NC
  • ON = COM is connected to NO

- Also, a diode is used to protect the transistor from the voltage spike generated when power is disconnected from the coil, what could damage the transistor. One leg of the diode is connected to the transistor collector and the other to the coil.

2 - Code

- Arduino sketch:

- Let's analyze the code.

- First of all, the transistor base is assigned to the Arduino pin 4 in output mode:

- Now, the transistor base drives current (pin 4 is set to HIGH) energizing the coil and the switch contacts are closed. As a consequence COM connects to NO and the relay is ON for 2 seconds (the red LED lights). 

- Then, when the transistor base is LOW the COM connects to NC and the relay puts into OFF mode for another 2 seconds (the yellow LED lights):

3 - Testing the circuit and the code

- In this video the circuit and the code are tested. The 2 LEDs light alternatively every two seconds and the relay contacts "click" is heard very clearly:

Thursday, July 6, 2017

5 - Arduino: Water level sensor


- Layout for this exercise:

- The goal of this exercise is to use a water level sensor with an Arduino board that could be useful for detecting water level presence in different scenarios like a basement or facility flood, outdoor rain sensors, etc ...

- As seen in the picture the water sensor has got 3 pins connected to the Arduino board in this way:

  • VCC = 5 V
  • GND = GND
  • S (sensor) = A0 (Arduino analog input) 

- Code for the circuit:

- The code takes the reading from the sensor S what is directly connected to the Arduino analog input A0.

- According with the reading value the calibration goes from 0 to more than 700 dividing into four different intervals.

- Starting from "No water" (the sensor is not in contact with water), and after introducing the sensor into a vessel filled with water, the serial output indicates the change immediately and the corresponding water level height:

- Testing the circuit:

Wednesday, July 5, 2017

4 - Arduino: Hygrometer for sensing soil moisture


- Layout for this exercise:

- The goal of this exercise is to build an Arduino circuit able to detect moisture levels, for instance it could be used for a pot of plants.

- The circuit will trigger a couple of alarms (buzzer sound and red LED) whenever the moisture level is under a moisture threshold specified at the Arduino sketch.

- There are several moisture sensor available in the market, for instance the HL-69, YL-69, FC-28, ..., all of them compatible with the Arduino board.

- A moisture sensor consists of two parts: a prong sensor and a controller.

- On one side the controller is directly connected to the sensor, on the other side it is connected to the Arduino board:

  • AO (Analog Output) -> A0
  • DO (Digital Output) -> not used
  • GND (Ground) -> GND
  • VCC -> +5 V

- Code:

- Let's analyze the code.

- First of all, the readings are taken from the analog A0 pin connected to the controller:

- Because the range of digital conversion at the analog input A0 is from 0 to 1023, let's calibrate the reading so that 0 corresponds to 100% and 1023 to 0% of moisture:

- When moisture is below 20% the two alarms are triggered: a buzzer produces a sound of 2000 Hz and a red LED blinks:

- Also, the serial ouput prints the moisture level on the screen every 2 seconds, whenever a new reading is made. In this picture the % of moisture varies abruptly when the sensor detects water:

- Testing the circuit, when the sensor is touching dry soil the buzzer beeps and the red LED blinks. However, if water is added to the soil both the beep and red light stop. Also, a message is printed through the serial monitor displaying the increase in humidity level:

Saturday, July 1, 2017

3 - Arduino: setting alarms for a Temperature Sensor


- Layout for this exercise:

1 - Introduction

- The goal of this exercise is to trigger 3 alarms when a temperature threshold is exceeded.

- For that purpose an Arduino microcontroller is connected to 4 electronic devices:

a) TMP36: precision temperature sensor that measures the temperature of the environment
b) red LED: blinks when the temperature goes above a limit
c) serial monitor: prints messages on the serial terminal
d) buzzer or piezo element: beeps when the temperature goes above a limit

2 - Code

- This is the code used in this exercise:

3 - Analysis of the code

- The temperature sensor is connected to the input analog A0 pin which converts the voltage output of the sensor to digital values, and finally translating into Farenheit degrees:

- Setting a temperature threshold, three events happen if the threshold is exceeded:

a) a beep of 2000 Hz and 500 ms:

b) a red LED blinks:

c) a warning message is printed at the serial monitor:

- The serial monitor is initialized to 9600 bauds:

- The series of Farenheit temperatures is normally displayed at the monitor, until a warning message (WARNING, VERY HOT !!) is triggered when the temperature goes above 100.00 F:

4 - Testing the circuit

- In this video it can be checked how the 3 alarms (red LED, buzzer beep and monitor message) go off when a hot air jet from a hair dryer is blown over the circuit, increasing the temperature above 100 F:

2 - Arduino: piezoelectric buzzer playing Jingle Bells


1 - Introduction

-  A buzzer is an electromechanical component that can be used to make noise or even play music.

- Inside the buzzer there is a coil of wire and a small magnet. When current flows through the coil it becomes magnetized and pulls towards the magnet creating a tiny "click". When the click is played thousands of times per second tones are created.
- The Arduino has a built-in command called tone() which clicks the buzzer at a certain frequency. 

- Frequencies used in this exercise and their corresponding notes:

x (g for a lower octave) = 196 Hz
y (a for a lower octave) = 220 Hz
c = 261 Hz
d = 293 Hz
e = 329 Hz
f = 349 Hz
g = 392 Hz
a = 440 Hz

2 - Layout schematics

 - The buzzer has two pins, one positive (marked by a "+" symbol) and the other negative.

 - In this example the positive pin is connected to Arduino digital pin 9 (optionally through a resistor of 330 Ohms), and the negative pin is connected to GND.

3 - Running the Arduino sketch

- The code used in this Arduino sketch:

- Running the code over the circuit (the resistor of the previous schematics has been removed to obtain louder sound from the buzzer):

1 - Arduino: Servo and DC Spinning motors


1 - Servomotor

- Layout for this exercise:

- The goal of this exercise is to make rotate a servo motor back and forth through the full range of motion, what is  180 degrees.

- A servo motor include feedback circuitry that allows it to be commanded to specific positions, what makes it very useful in robotics. 

- The servo has got 3 wires that can be connected with jumper wires: black goes to ground, red goes to 5 V and yellow goes to the Arduino 6 digital pin.

- Code:

- Let's analysis the code.

- First of all the servo library is included, so that all servo functions can be easily accessed. Also, a servo object called "s" is defined:

- Now, the servo object is attached to the Arduino 6 digital pin:

- The movement goes from 0 to 180 degrees:

- The first rotation takes 100 ms delay between each move, stepping 1 degree:

- 1 second of delay between the two rotations:

- The second rotation is faster because there is just 20 milliseconds between each move:

- Testing the circuit:

2 - DC Spinning Motor

- Layout for this exercise:

- The goal of this exercise is to connect a DC motor to an Arduino circuit so that the motor starts spinning and accelerating up to a maximum speed and then decelerates until stopping.

- Two electronic components are used:

a) Transistor: it helps to run the DC motor, because the motor needs more current (even  100 or 250 mA) than provided by the Arduino pins (40 mA). The transistor has 3 pins (Collector: connected to the motor, Base: connected to the resistor, Emitter: connected to GND).

 b) Diode: when the motor is spinning and suddenly turned off, the magnetic field inside it collapses, generating a voltage spike that can damage the transistor. To prevent this, a diode is used, diverting the voltage spike away from the transistor. The diode has 2 pins (Cathode: connected to 5 V, Anode: connected to the transistor)

- Code:

- Let's analyse the code.

- The 10 pin is assigned as the motorPin and oputput mode. Also, the serial monitor is initialized with 9600 bauds, inviting the user to enter a speed inside the 0 to 255 interval:

- The analogWrite() function pulses the motorPin using the PWM(Pulse Width Modulation) technique,  varying the width of the pulse from 0% to 100%. 

- The motor accelerates with 10 milliseconds delay between each move:

- Same thing for the decelerating process:

- Once uploaded the sketch to the board the user enters the rotation speed through the serial monitor interface:

- Testing the circuit: