The light-emitting diode also called LED is a special type of diodes which emits light, unlike the other diodes. LED has a completely different body which is made of transparent plastic that protects the diode and lets it emit light. Like the other diodes LED conducts the current in only one way, so it is essential to connect it to the scheme correctly. There are two safe ways how to determine the direction of the diode:
The LED is one of the best light sources. Unlike incandescent light bulb LED transforms most of the power into light, not warmth; it is more durable, works for a more extended period and can be manufactured in a smaller size.
The LED colour is determined by the semiconductors material. Diodes are usually made from silicon then LEDs are made from elements like gallium phosphate, silicon carbide and others. Because the semiconductors used are different, the voltage needed for the LED to shine is also different. In the table, you can see with which semiconductor you can get a specific colour and the voltage required to turn on the LED.
When LED is connected to the voltage and turned on a huge current starts to flow through it, and it can damage the diode. That is why all LEDs have to be connected to current limiting resistor.
Current limiting resistors resistance is determined by three parameters:
To calculate the resistance needed for a diode, this is what you have to do.
The example of the blinking LED code:
int ledPin = 8;//Defining the pin of the LED void setup() { pinMode(ledPin,OUTPUT); //The LED pin is set to output } void loop() { //Set pin output signal to HIGH – LED is working digitalWrite(ledPin,HIGH); //Belay of 1000 ms delay(1000); //Set pin output signal to LOW – LED is not working digitalWrite(ledPin,LOW); //Delay of 1000 ms delay(1000); }
Using display is a quick way to get feedback information from the device. There are many display technologies compatible with Arduino. For IoT solutions, low power, easy to use and monochrome displays are used:
Liquid-Crystal Display (LCD)
LCD uses modulating properties of liquid crystal light to block the incoming light. Thus when a voltage is applied to a pixel, it has a dark colour. A display consists of layers of electrodes, polarising filters, liquid crystals and reflector or back-light. Liquid crystals do not emit the light directly; they do it through reflection or backlight. Because of this reason, they are more energy efficient. Small, monochrome LCDs are widely used in devices to show a little numerical or textual information like temperature, time, device status etc. LCD modules commonly come with an onboard control circuit and are controlled through parallel or serial interface.
The example code:
#include <LiquidCrystal.h> //include LCD library //Define LCD pins const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2; //Create and LCD object with predefined pins LiquidCrystal lcd(rs, en, d4, d5, d6, d7); void setup() { lcd.begin(16, 2); //Set up the LCD's number of columns and rows lcd.print("hello, world!"); //Print a message to the LCD } void loop() { //Set the cursor to column 0, line 1 – line 1 is the second row //Since counting begins with 0 lcd.setCursor(0, 1); //Print the number of seconds since reset lcd.print(millis() / 1000); }
Organic Light-Emitting Diode Display (OLED)
OLED display uses electroluminescent materials that emit light when the current passes through these materials. The display consists of two electrodes and a layer of an organic compound. OLED displays are thinner than LCDs, they have higher contrast, and they can be more energy efficient depending on usage. OLED displays are commonly used in mobile devices like smartwatches, cell phones and they are replacing LCDs in other devices. Small OLED display modules usually have an onboard control circuit that uses digital interfaces like I2C or SPI.
//Add libraries to ensure the functioning of OLED #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define OLED_RESET 4 Adafruit_SSD1306 display(OLED_RESET); void setup() { //Setting up initial OLED parameters display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false); display.setTextSize(1); //Size of the text display.setTextColor(WHITE); //Colour of the text – white void loop() { //Print out on display output sensor values display.setCursor(0, 0); display.clearDisplay(); display.print("Test of the OLED"); //Print out the text on the OLED display.display(); delay(100); display.clearDisplay(); }
Electronic Ink Display (E-Ink)
E-ink display uses charged particles to create a paper-like effect. The display consists of transparent microcapsules filled with oppositely charged white and black particles between electrodes. Charged particles change their location, depending on the orientation of the electric field, thus individual pixels can be either black or white. The image does not need the power to persist on the screen; power is used only when the image is changed. Thus e-ink display is very energy efficient. It has high contrast and viewing angle, but it has a low refresh rate. E-ink displays are commonly used in e-riders, smartwatches, outdoor signs, electronic shelf labels.
#include <SmartEink.h> #include <SPI.h> E_ink Eink; void setup() { //BS LOW for 4 line SPI pinMode(8,OUTPUT); digitalWrite(8, LOW); Eink.InitEink(); Eink.ClearScreen();//Clear the screen Eink.EinkP8x16Str(14,8,"NOA-Labs.com"); Eink.EinkP8x16Str(10,8,"smart-prototyping.com"); Eink.EinkP8x16Str(6,8,"0123456789"); Eink.EinkP8x16Str(2,8,"ABCDEFG abcdefg"); Eink.RefreshScreen(); } void loop() { }
Relays are electromechanical devices that use electromagnets to connect or disconnect plates of a switch. Relays are used to control high power circuits with low power circuits. Circuits are mechanically isolated and thus protect logic control. Relays are used in household appliance automation, lighting and climate control.
The example code:
#define relayPin 4 //Define the relay pin void setup() { Serial.begin(9600); pinMode(relayPin, OUTPUT); //Set relayPin to output } void loop() { digitalWrite(relayPin,0); //Turn relay on Serial.println("Relay ON"); //Output text delay(2000); // Wait 2 seconds digitalWrite(relayPin,1); //Turns relay off Serial.println("Relay OFF"); delay(2000); }
Solenoids are devices that use electromagnets to pull or push iron or steel core. They are used as linear actuators for locking mechanisms indoors, pneumatic and hydraulic valves and in-car starter systems.
Solenoids and relays both use electromagnets and connecting them to Arduino is very similar. Coils need a lot of power, and they are usually attached to the power source of the circuit. Turning the power of the coil off makes the electromagnetic field to collapse and creates very high voltage. For the semiconductor devices protection, a shunt diode is used to channel the overvoltage. For extra safety, optoisolator can be used.
The example code:
#define solenoidPin 4 //Define the solenoid pin void setup() { Serial.begin(9600); pinMode(solenoidPin, OUTPUT); //Set solenoidPin to output } void loop() { digitalWrite(solenoidPin,0); //Turn solenoid on Serial.println("Solenoid ON"); //Output text delay(2000); //Wait 2 seconds digitalWrite(solenoidPin,1); //Turns solenoid off Serial.println("Solenoid OFF"); delay(2000); }
Speakers are electroacoustic devices that convert the electrical signal into sound waves. A speaker uses a permanent magnet and a coil attached to the membrane. Sound signal, flowing through the coil, creates the electromagnetic field with variable strength, coil attracts to magnet according to the strength of the field, thus making a membrane to vibrate and creating a sound wave. Other widely used speaker technology, called Piezo speaker, uses piezoelectric materials instead of magnets. Speakers are used to creating an audible sound for human perception and ultrasonic sound for sensors and measurement equipment.
const int speakerPin = 9; //Define the buzzer pin void setup() { pinMode(speakerPin, OUTPUT); //Set buzzer as an output } void loop() { tone(speakerrPin, 1000); //Send 1 kHz sound signal delay(1000); //For 1 s noTone(speakerPin); //Stop sound delay(1000); //For 1 s }
An electric motor is an electro-technical device which can turn electrical energy into mechanical energy; motor turns because of the electricity that flows in its winding. Electric motors have seen many technical solutions over the year from which the simplest is the permanent-magnet DC motor.
DC motor is a device which converts direct current into the mechanical rotation. DC motor consists of permanent magnets in stator and coils in the rotor. By applying the current to coils, the electromagnetic field is created, and the rotor tries to align itself to the magnetic field. Each coil is connected to a commutator, which in turns supplies coils with current, thus ensuring continuous rotation. DC motors are widely used in power tools, toys, electric cars, robots, etc.
void setup () { pinMode(5,OUTPUT); //Digital pin 5 is set to output //The function for turning on the motor is defined #define motON digitalWrite(5,HIGH) //The function for turning off the motor is defined #define motOFF digitalWrite(5,LOW) } void loop () { motON; //Turn on the motor }
The H-bridge has earned its name because of its resemblance to the capital ‘H’ wherein all the corners there are switches and in the middle – the electric motor. This bridge is usually used for operating permanent-magnet DC motor, electromagnets and other similar elements, because it allows operating with significantly bigger current devices, using a small current. By switching the switches, it is possible to change the motor direction. It is important to keep in mind that the switches need to be turned on and off in pairs.
When all of the switches are turned off, the engine is in the free movement. To slow down faster, the H-bridge is turned on in the opposite direction.
If both positive or both negative switches are turned on at the top or at the bottom, then the engine stops, not allowing to have a free rotation – it is slowed down. The management can be reflected in Table 10.
When all of the switches are turned off, the engine is in the free movement. Not always it is enough for robotics, so sometimes the H-bridge is turned on in the opposite direction to slow the motor down faster – the opposite direction is turned on rapidly.
Remember! Neither of these braking mechanisms is good for the H-bridge or the power source. That is why this action is unacceptable without a particular reason because it can damage the switches or the power source.
The complicated part is the realisation of switches – if the switches don’t work usually relays or appropriate power transistors are used. The biggest drawback for relays is that they can only turn the engine on or off. If the rotation speed needs to be regulated using the impulse width modulation, then transistors have to be used. MOSFET type transistors should be used for ensuring a large amount of power. Nowadays, the stable operation of the bridge is ensured by adding extra elements. The manufactured bridges have one body, for example, the one that is included in the constructor – L293D.
The L293D microchip consists of two H-bridges and is made for managing two motors. Each pin of the microchip has its function; that is why it is very important not to mix them up; otherwise, the microchip can be damaged. All pins of the microchip have assigned a number. The enumeration begins with the special mark on the body: a split, a dot, a cracked edge, etc., and continues counter-clockwise. When creating a scheme, it is important to take into account pin numbers and the ones shown in the scheme. If some additional information about the microchip is necessary, it can be found in the datasheet of the microchip. Remember that the datasheet can be found by writing the number of the device (written on the body) and adding the word “datasheet” in the browser.
The example code:
int dirPin1 = 7; //1st direction pin int dirPin2 = 8; //2nd direction pin int speedPin = 5; //Pin responsible for the motor speed void setup () { pinMode (dirPin1,OUTPUT); //1st direction pin is set to output pinMode (dirPin2,OUTPUT); //2nd direction pin is set to output pinMode (speedPin,OUTPUT); //Speed pin is set to output } void loop () { analogWrite(speedPin, 100); //Setting motor speed //Speed value can be from 0 to 255 int motDirection = 1; //Motor direction can be either 0 or 1 if (motDirection) //Setting motor direction {//If 1 digitalWrite(dirPin1,HIGH); digitalWrite(dirPin2,LOW); } else {//If 0 digitalWrite(dirPin1,LOW); digitalWrite(dirPin2,HIGH); } }
Stepper motors are motors, that can be moved by a certain angle or step. Full rotation of the motor is divided into small, equal steps. Stepper motor has many individually controlled electromagnets, by turning them on or off, a motor shaft rotates by one step. Changing switching speed or direction can precisely control turn angle, direction or full rotation speed. Because of exact control ability, they are used in CNC machines, 3D printers, scanners, hard drives etc. Example of use can be found in the source [1].
The example code:
#include <Stepper.h> //Include library for stepper motor int in1Pin = 12; //Defining stepper motor pins int in2Pin = 11; int in3Pin = 10; int in4Pin = 9; //Define a stepper motor object Stepper motor(512, in1Pin, in2Pin, in3Pin, in4Pin); void setup() { pinMode(in1Pin, OUTPUT); //Set stepper motor control pins to output pinMode(in2Pin, OUTPUT); pinMode(in3Pin, OUTPUT); pinMode(in4Pin, OUTPUT); Serial.begin(9600); motor.setSpeed(20); //Set the speed of stepper motor object } void loop() { motor.step(5); //Rotate 5 steps }
Unlike the simple DC motor, the servomotor is a particular management chain which allows effortless control over the speed or position of the motor. The management of the engine is realised using three connections – positive (usually red) and negative connection (brown or black) of the current as well as the connection for management (orange or yellow).
For the management, the pulse width technique is used.
From the image, it can be seen that the length of the servomotor impulse cycle is 20 ms, but the impulse length itself is 1 ms or 2 ms. These signal characteristics are true for the most enthusiast level servomotors, but it should be verified for each module in the manufacturer specification. Servomotor management chain meets the impulse every 20 ms, but the width of the pulse shows the position that the servomotor has to reach. For example, 1 ms corresponds to the 0° position but 2 ms – to 180° position against the starting point. When entering the defined position, the servomotor will keep it and resist any outer forces that are trying to change the current position. The graphical representation is in the following image.
Just like other motors, servomotors have different parameters, where the most important one is the time of performance – the time that is necessary to change the position to the defined position. The best enthusiast level servomotors do a 60° turn in 0.09 s. There are three types of servomotors:
Unfortunately, using Arduino, the servomotor is not as easily manageable as DC motor. For this purpose, a special servomotor management library Servo.h has been created.
The example code:
#include <Servo.h> //Include Servo library Servo servo; //Define a Servo object void setup () { servo.attach(6); //Connect servo object to pin D6 servoLeft.write(90); //Set position of servo to 90° Serial.begin(9600); } void loop () { servoLeft.write(110); //Set position of servo to 110° delay(200); //wait for 200 ms servoLeft.write(70);//Set position of servo to 70° delay(200); //Wait for 200 ms }