Chapter 5
Starter Kit with Arduino Uno R3
In this beginner Kit with Arduino Uno R3, you will explore lots of lessons. It has everything you need to perform all the lessons in this chapter and to start developing your own projects with the Arduino platform, without the need for welding.
Lessons
Lessons developed as an example to use all the components of your Beginner Kit with Arduino Uno R3. In each lesson, you identify the materials needed for its execution, the previous knowledge needed, and what you will learn, the assembly diagram, the programming code, tips, and extra exercises.
Hello World - Blinking
This example shows the most uncomplicated experience you can do with an Arduino to check a physical output: blink an LED.
When you're learning how to program, in most programming languages, the first code you write says "Hello World" on your computer screen. Since the Arduino board does not have a display, we will replace this function by flashing an LED.
What Will I Learn?
Previous Knowledge
Required Materials
-
1 Arduino Uno1 LED
-
1 AB USB cable
This code already comes with the Arduino IDE. You can access in: Archive > Examples > 01. Basics > Blink
We only reproduce here with explanations and comments.
In the following program, the first command is to initialize pin 13 as an output through the line.
pinMode(13, OUTPUT);
In the main loop of the code, you turn on the LED with this command line:
digitalWrite(13, HIGH);
This command directs 5 volts to pin 13 and lights it up. You switch off the LED with the following command:
digitalWrite(13, LOW);
This command removes the 5 volts from pin 13, returning to 0 and turning off the LED. Between power off and on you need enough time for one person to see the difference, so the delay() command tells Arduino not to do anything for 1000 milliseconds, or one second. When you use the delay() command, nothing else happens in this period. Once you have understood the basic examples.
Source Code
/ *
Blinking
Illuminates an LED for one second, and then goes out for the same time repeatedly.
* /
// Sets a name for pin 13: int led = 13;
// It is executed each time Arduino starts: void setup () {
// Initializes the digital pin as output. pinMode (led, OUTPUT);
}
// The loop () function continues to perform while Arduino is powered,
// or until the reset button is pressed.
void loop () {
digitalWrite (led, HIGH); // Lights up the LED
delay (1000); // Wait one second (1s = 1000ms) digitalWrite (led, LOW); // Turn off the LED
delay (1000); // Wait one second (1s = 1000ms)
}
Tips
1. In the Arduino // language it is used to add comments to the line of code, being very useful to explain a syntax or leave a reminder. An example of its use:
digitalWrite(13,LOW);
//Deletes the LED
2. Digital signals (ON and OFF) are present in many sensors. Meet some of them:
Exercise 1
From the source code presented in this lesson, make the necessary modifications to make the LED stay:
Exercise 2
From the same source code, make a new assembly of this lesson and make the necessary modifications to the source code so that the LED is placed on Pin 5 and is on for 2 seconds and off for 1 second.
Note that for any pin other than 13, it is necessary to place a resistor in series with O LED, In this case, a resistor from 330Ω is sufficient.
Button
The button is a component that connects two points of the circuit when it is pressed. In this example, when the button is pressed, the LED lights up.
What Will I Learn?
Previous Knowledge
Required Materials
-
1 Arduino Uno
-
1 Button
-
1 LED
-
1 Resistor 10kΩ
-
1 AB USB cable Jumpers
-
1 Protoboard
Source Code
/ *
Button
Turns on and off an LED connected to digital pin 13 when a button connected to pin 2 is pressed.
The Circuit:
* LED connected to pin 13 and ground
* button connected to pin 2 from 5V
* 10K resistor connected to pin 2 from ground
* /
// constants are not changed.
// These are used here to define the pin numbers: const int buttonPin = 2; // the pin number of the const int ledPin button = 13; // the pin number of the LED pin
// variables that must change:
int buttonState = 0; // variable to read the state of the button
void setup () {
// initializes the LED pin as output: pinMode (ledPin, OUTPUT);
// initializes the button pin as input: pinMode (buttonPin, INPUT);
}
void loop () {
// reads the button value: buttonState = digitalRead (buttonPin);
// check if the button is pressed.
// if yes, buttonState and HIGH: if (buttonState == HIGH) {
// turn on the LED: digitalWrite (ledPin, HIGH);
}
else {
// turns off the LED: digitalWrite (ledPin, LOW);
}
}
Tips
1. When you are programming with the Arduino software, many of the words you write are reserved for the language. These words are placed in a different color, and it is a hint to check if they are written correctly. As in the example:
void loop(){ digitalWrite(13,HIGH); delay(1000); digitalWrite(13,LOW); delay(1000);
}
2. In a project with the use of several buttons with different functionalities, it can be useful to work with parts like these:
Colorful touch button set.
Exercise 1
To prevent accidents in the workplace, a safety rule in several industrial types of equipment is to require a user to press two buttons, one with each hand, to drive a machine. This is the case with the cutting machine used in paper mills.
With the following assembly, we can simulate this situation. The LED only illuminates if both circuit buttons are pressed:
Exercise 2
Make one more change to the source code of exercise 1 so that you can light the LED on pin 13 by pressing either button 1 or button 2. When you stop pressing, the LED goes out.
Serial Reading of a Digital Input
This example shows how to monitor the status of a switch by establishing serial communication between your Arduino and the computer via USB.
What will you learn?
Previous Knowledge
Required Materials
-
1 Arduino Uno
-
1 Button
-
1 LED
-
1 Resistor 10kΩ
-
1 AB USB cable Jumpers
-
1 Protoboard
Source Code
In this program, the first thing you will do in the configuration function is to start the serial communication at 9600 data bits per second between Arduino and his computer: Serial.begin (9600);
Remember to place the value 9600 also in the Serial Monitor, as explained earlier.
Then initialize the digital pin 2, the pin that will scan the button as a digital input:
int pushButton = 2;
When the button is pressed, 5 volts will flow freely through its circuit, and when not pressed, the input pin will be grounded. This is a digital input, which means that the key can only have one state (seen by your Arduino as "1", or HIGH) or an off state (seen by your Arduino as a "0", or LOW), with nothing in between.
Now when you open your Serial Monitor in the Arduino environment you will see a stream of "0". if your key is open, or "1" if your key is closed.
/ *
DigitalReadSerial
Read the digital input on pin 2 and print the result on the serial monitor. This example is a public domain.
* /
int pushButton = 2; // pin 2 has a button connected to it. int ledPin = 13; // LED input on pin 13.
void setup () {
// Initialize serial communication at 9600 bits per second: Serial.begin (9600);
pinMode (pushButton, INPUT); // define the button as an input. pinMode (ledPin, OUTPUT); // set the LED as an output.
}
void loop () {
// reads the input pin:
int buttonState = digitalRead (pushButton); if (buttonState == 1) {
digitalWrite (ledPin, HIGH);
} else {digitalWrite (ledPin, LOW);
}
// prints the state of the button: Serial.println (buttonState);
delay (1); // Delay between readings (in milliseconds)
}
Tips
1. The binary system is a positional numbering system in which all quantities are represented based on two numbers, i.e., zero and one (0 and 1). Computers work internally with two voltage levels, so their natural numbering system is the binary system (on, off).
The binary system is the basis for Boolean algebra that allows logical and arithmetic operations using only two digits or two states (yes and no, false and true, all or nothing, 1 or 0, on and off). All digital electronics and computing are based on this binary system and Boole's logic, which allows representing by digital electronic circuits (logical gates) the numbers, characters, perform logical and arithmetic operations. Computer programs are encoded in binary form and stored in media (memories, disks, etc.) under this format.
2. To remember:
-
To read a digital signal use: digitalRead(numeroPin).
-
To write a digital signal use: digitalWrite(numeroPin, value).
-
A digital output or input is always HIGH or LOW.
Exercise 1
Here we will do one more exercise using the same assembly of this Lesson.
Once you have the button working, you often want to do some action based on the number of times the button is pressed. To do this, you need to know when the button changes from off to on, and count how many times this change of state happens. This is called state change detection. Every 4 pulses, the LED will turn on.
/ *
Pulse counter (edge detection)
created on 9/27/2005, modified on 8/30/2011 by Tom Igoe. This example is from the public domain. http://arduino.cc/en/Lesson/ButtonStateChange
* /
// constants are not changed:
const int buttonPin = 2; // the pin number of the const int ledPin button = 13; // the number of the LED pin
// variables that must change:
int buttonPushCounter = 0; // counter for the number of prints of the int button buttonState = 0; // current status of the button
int lastButtonState = 0; // previous button state
void setup () {
pinMode (buttonPin, INPUT); // initialize the button pin as input pinMode (ledPin, OUTPUT); // initialize the digital pin as output Serial.begin (9600); // initialize the serial communication
}
void loop () {
// reads the button value: buttonState = digitalRead
(buttonPin);
// compare the current state of the button with its previous state if (buttonState! = lastButtonState) {
// if the button status has been changed, increment the if counter (buttonState == HIGH) {
buttonPushCounter ++; Serial.print ("number of pulses:"); Serial.println (buttonPushCounter);
}
}
// saves the current state of the button as the last state to start the
// next loop
lastButtonState = buttonState;
// Turn on the LED every 4 pulses by checking the if button counter module (buttonPushCounter% 4 == 0) {
digitalWrite (ledPin, HIGH);
It is {digitalWrite (ledPin, LOW);
}
}
Serial Reading of an Analog Input
This example shows how to read a pin from an analog input, map the result to a range of 0 to 255, and use that result to set the PWM modulation of an output pin to turn an LED on and off as a dimmer.
What Will I Learn?
Previous Knowledge
Required Materials
-
1 Arduino Uno
-
1 LED
-
1 Resistor 330Ω
-
1 Potentiometer
-
1 AB USB cable Jumpers
-
1 Protoboard
Source Code
/ *
Analog Input, Analog Output, Serial Output
Read the analog input pin, map the result to a range between 0 and 255, and use the result to establish the PWM pulse of the output pin.
It is also possible to follow the result through the Serial Monitor.
The circuit:
- The central pin of the potentiometer connected to the analog pin 0. The side pins of the Potentiometer connected to ground and 5V.
- LED connected to digital pin 9 and ground.
Created on 29/12/2008, Modified on 04/04/2012 by Tom Igoe
This example is a public domain.
* /
// constants are not changed:
const int analogInPin = A0; // Potentiometer analog input
const int analogOutPin = 9; // Analog output where the LED is connected
int sensorValue = 0; // potentiometer reading
int outputValue = 0; // PWM output reading (analog)
void setup () {
// Initialize serial communication: Serial.begin (9600);
}
void loop () {
// sensorValue = analogRead (analogInPin);
// maps the result of the analog input within the range of 0 to 255: outputValue = map (sensorValue, 0, 1023, 0, 255);
// changes the value of the analog output: analogWrite (analogOutPin, outputValue);
// prints the result on the serial monitor: Serial.print ("sensor ="); Serial.print (sensorValue); Serial.print ("\ t output ="); Serial.println (outputValue);
// Wait 2 milliseconds before the next loop: delay (2);
}
Tips
1. Note that the analog inputs of Arduino have a resolution of 10 bits (values from 0 to 1023), but the analog outputs by PWM have a resolution of 8 bits (values from 0 to 1023).
255). This is why you need the 'map' function to 'map' the values so that they remain proportional.
2. Other elements that are also potentiometers:
Command with Serial Communication
Through this lesson, you will control the activation of a relay and an LED from the Serial Monitor of your computer.
What Will I Learn?
Previous Knowledge
-
Boolean Variables
-
Serial.print
Required Materials
-
1 Arduino Uno
-
2 LEDs
-
1 Resistor 330Ω
-
1 Relay
-
1 AB USB cable Jumpers
-
1 Protoboard
Source Code
// ************************************************ **************************
// * Code for testing Arduino Multilogic triggering relay kit,
// * connected to digital output 2 and GND, monitored by LED 13
// * this code has public domain
// ************************************************ **************************
// initialize a char type variable that uses 1 byte to store
// 1 character char input = 0; int rele = 2; int led = 13;
boolean y = true; // initializes a boolean type variable
void setup () {pinMode (relay, OUTPUT); pinMode (led, OUTPUT);
Serial.Begin (9600); Serial.println ();
Serial.print ("** Code to trigger relay connected to Arduino pin 2"); Serial.println ("via serial monitor **");
Serial.println ("");
Serial.println ("Press 1 and then ENTER to invert the relay state again");
Serial.println ("Awaiting command:");
}
void loop () {
if (Serial.available ()> 0) {input = Serial.read ();
if (input == '1') {
Serial.print ("The re-read is now");
if (y) {
digitalWrite (relay, HIGH); digitalWrite (led, HIGH); Serial.println ("on");
}
else {
digitalWrite (relay, LOW); digitalWrite (led, LOW); Serial.println ("off");
}
y =! y; // alters or value of y, le and e equal to not
}
else {
Serial.println ("Invalid command");
}
}
}
Fade
This example demonstrates the use of the analogWrite() function to turn off a fade LED. AnalogWrite uses a PWM pulse, switching the digital pin on and off quickly, creating a fade effect.
What Will You Learn?
Previous Knowledge
Required Materials
-
1 Arduino Uno
-
1 LED
-
1 Resistor 330Ω
-
1 AB USB cable Jumpers
-
1 Protoboard
Source Code
/*
Fade
This example shows how to perform a fade on an LED on pin 9 using the analogWrite() function.
This example is a public domain
* /
int led = 9; // LED pin
int brightness = 0; // LED brightness intensity
int fadeAmount = 5; // on how many points to apply the fade on the LED
void setup () {
// sets pin 9 as output:
pinMode (led, OUTPUT);
}
// the loop runs in sequence continuously: void loop () {
// sets the brightness of pin 9:
analogWrite (led, brightness);
// changes the brightness for the next loop: brightness = brightness + fadeAmount;
// reverses the direction of the fade at the end of the fade:
if (brightness == 0 || brightness == 255) {fadeAmount = -fadeAmount;
}
// wait 3
0 milliseconds to see the dimmer effect: delay (30);
}
Tips
Connect the most extended leg of the LED to the digital pin 9 of your Arduino via a
330Ω. Connect the shortest leg of the LED directly to the ground.
After defining that pin 9 will be your ledPin, nothing more should be done in the setup() function of the code.
The analogWrite() function that you will use in the main loop of the code requires two arguments: one telling the function which pin to trigger and the other indicating which PWM value to use.
To run the fade on the LED, gradually increase the PWM value from 0 (entirely off) to 255 (fully on) and then decrease again to 0 to complete the cycle. In the code below, the PWM value is set using a variable called brightness. Each time the loop rotates, it increases the value of the variable according to the fadeAmount.
If brightness is set between the extreme values (0 or 255), then fadeAmount changes to its negative. For example, if fadeAmount is 5, then it is set to -5. If it is -5, then it would be set to 5. The next time you rotate the loop, this change causes the brightness increment also to change direction.
analogWrite() can change the PWM value very quickly, so the delay at the end of the code controls the fade speed. Try to modify the delay value and see how this changes the program.
Loop
Many times you want to repeat an action on a series of pins and do something different for each one. In this case, the example flashes 6 LEDs using the for() loop function to circulate back and forth between pins 2 and 7. The LEDs turn on and off in sequence, using both the digitalWrite() and delay() functions.
We can call this example "Super Machine" by recalling the television series of the 80s in which the famous actor David Hasselhoff directed his Pontiac with artificial intelligence. The car was turbocharged with various LEDs of various possible sizes to reproduce bright effects.
We thought it would be interesting to use this metaphor of the "Super Machine" to learn more about sequential programming and good programming techniques for the I/O information on the board.
What Will You Learn?
-
for() loop function
-
digitalWrite()
-
delay()
Required Materials
-
1 Arduino Uno6 LEDs
-
1 ABJumpers USB cable
-
6 Resistors 330Ω
Source Code
The code below starts using the for() loop function to designate digital pins 2 to 7 as outputs of the 6 LEDs used. In the main loop of the code, two for() loops are used to increase the loop by going through the LEDs, one by one, from pin 2 to pin 7. Once pin 7 is lit,
the process reverses, going back through each LED.
For more information on the for() function, see page 82.
/ *
Loop
Demonstrates the use of the for () loop function.
It lights up several LEDs in sequence, and then the coating.
The circuit:
* LEDs between pins 2 to 7 and ground
Created in 2006 by David A. Mellis
Modified on August 30, 2011, by Tom Igoe
This code is a public domain. http://www.arduino.cc/en/Lesson/ForLoop
* /
int timer = 100; // The higher the value, the slower the sequence of LEDs. void setup () {
// Use for loop to initialize each pin as output: for (int thisPin = 2; thisPin <8; thisPin ++) {
pinMode (thisPin, OUTPUT);
}
}
void loop () {
// loop from lowest to highest pin: for (int thisPin = 2; thisPin <8; thisPin ++) {
// Connect this pin: digitalWrite (thisPin, HIGH); delay (timer);
// turns off this pin: digitalWrite (thisPin, LOW);
}
// loop from highest to lowest pin: for (int thisPin = 7; thisPin>
= 2; thisPin--) {
// Connect this pin: digitalWrite (thisPin, HIGH); delay (timer);
// turns off this pin: digitalWrite (thisPin, LOW);
}
}
LDR Sensor
In this lesson, we will use an LDR (Light Dependent Resistor) to simulate a light compensation of 5 levels, that is, depending on whether there is more or less light focusing on the sensor, the system turns on or off a series of LEDs.
This program could be used in a lighting system with five lines of light that
they light up as the sun goes down, progressively compensating for the lack of light.
In addition, a potentiometer adjusts the minimum critical light level from which the circuit will activate.
What Will You Learn?
Previous Knowledge
-
DigitalWrite() function
-
Conditional if/else
Required Materials
-
1 Arduino Uno
-
1 LDR
-
5 LEDs
-
1 AB USB
-
5 Resistors 330Ω
Source Code
/ *
LDR sensor
Connect an LDR to an analog input to control five outputs depending on ambient light.
* /
// Save the data collected by the LDR sensor: intLDR value = 0;
// Set the input pins of the LEDs:
int Ledpin1 = 12; int Ledpin2 = 11; int Ledpin3 = 10; int Ledpin4 = 9; int Ledpin5 = 8;
// Set LDR sensor input pin
int pinLDR = 0;
void setup ()
{
Serial.Begin (9600);
// Set the LED output pins: pinMode (Ledpin1, OUTPUT); pinMode (Ledpin2, OUTPUT); pinMode (Ledpin3, OUTPUT);
pinMode (Ledpin4, OUTPUT); pinMode (Ledpin5, OUTPUT);
// Defined the use of an external reference:
analogReference (EXTERNAL);
}
void loop ()
{
// Save the reading value of a variable: LDR value = analogRead (pinLDR); Serial.println (LDR value);
// Definition of the control pattern of the LEDs:
if (LDR value> = 1023)
{
digitalWrite (Ledpin1, LOW); digitalWrite (Ledpin2, LOW); digitalWrite (Ledpin3, LOW); digitalWrite (Ledpin4, LOW); digitalWrite (Ledpin5, LOW);
}
else if ((LDR value> = 823) & (LDR value <1023)))
{
digitalWrite (Ledpin1, HIGH); digitalWrite (Ledpin2, LOW); digitalWrite (Ledpin3, LOW); digitalWrite (Ledpin4, LOW); digitalWrite (Ledpin5, LOW);
}
else if ((LDR value> = 623) & (LDR value <823)))
{
digitalWrite (Ledpin1, HIGH); digitalWrite (Ledpin2, HIGH); digitalWrite (Ledpin3, LOW);
digitalWrite (Ledpin4, LOW); digitalWrite (Ledpin5, LOW);
}
else if ((LDR value> = 423) & (LDR value <623)))
{
digitalWrite (Ledpin1, HIGH); digitalWrite (Ledpin2, HIGH); digitalWrite (Ledpin3, HIGH); digitalWrite (Ledpin4, LOW); digitalWrite (Ledpin5, LOW);
}
else if ((LDR value> = 223) & (LDR value <423)))
{
digitalWrite (Ledpin1, HIGH); digitalWrite (Ledpin2, HIGH); digitalWrite (Ledpin3, HIGH); digitalWrite (Ledpin4, HIGH); digitalWrite (Ledpin5, LOW);
}
else
{
digitalWrite (Ledpin1, HIGH); digitalWrite (Ledpin2, HIGH);
digitalWrite (Ledpin3, HIGH); digitalWrite (Ledpin4, HIGH); digitalWrite (Ledpin5, HIGH);
}
}
Tips
When the Arduino receives an analog signal, it converts it to digital in 1024 parts. This operation is standard since Arduino thinks that the signal he will receive varies between 0v and 5v, which gives us a value for each part of approximately 4.88 mV.
But we can say no, that really the system will work between 0v and 3v, thus getting 1024 parts distributed between 0v and 3v, which gives us a value for each part of 2.9 mV, i.e., a much higher resolution. The distribution of these values we will divide equally in our program to make a progressive activation of the lighting lines.
If we set the reference too low, the LEDs start working with less ambient light than if we set a higher signal, remember:
More light = less resistance = more Volt less light = more resistance = less Volt less
This control will be done via a potentiometer, where we can calibrate the system through the ambient light.
pinMode(EXTERNAL);
With this instruction, we are telling our Arduino not to use the reference voltage (+5V) but to apply it through the AREF pin.
Thermistor
In this lesson, you have to use a Thermistor (Temperature Dependent Resistor) to do a temperature reading.
The result, in degrees Celsius, we will see through the Serial Monitor of Arduino's IDE.
What Will I Learn?
Previous Knowledge
-
analogRead function
-
Serial.print
Required Materials
-
1 Arduino Uno
-
1 Thermistor
-
1 Resistor 1KΩ
-
1 AB USB cable Jumpers
-
1 Protoboard
Source Code
Note that it is not an accurate thermometer, just an approximate example based on empirical data.
Connect 1k resistor from A0 to ground and + 5V thermistor to A0 * /
#define pin_termistor A0
void setup (void) {Serial.begin (9600);
}
void loop (void) {float reading; float reading1;
reading = analogRead (pin_termistor); Serial.print ("Read pin A0 ="); Serial.println (read);
reading1 = (reading * 0.2027) -82; Serial.print ("Temperature approx. Celsius ="); Serial.println (reading1);
Serial.println (""); delay (2500);
}
Tips
There are two types of thermistors:
-
NTC (Negative Temperature Coefficient) - thermistors whose coefficient of resistance variation with temperature is negative: resistance decreases with temperature increase.
-
PTC (Positive Temperature Coefficient) - thermistors whose coefficient of resistance varies with the temperature is positive: the resistance increases with the increase of the temperature according to the characteristic curve/table of the thermistor, its resistance value can decrease or increase in a greater or lesser degree in a certain temperature range.
Thus, some can serve as overheat protection by limiting the electrical current when a certain temperature is exceeded. Another application is the measurement of temperature (in motors, for example) because we can, with the thermistor, obtain a variation of electrical resistance in the function of the variation of temperature.
DC Motor
In this lesson, we will control a DC motor through Arduino. The push of the button will start our engine.
What Will I Learn?
Previous Knowledge
-
DigitalWrite() function
-
DigitalRead() function
-
Conditional if/else
Required Materials
-
1 Arduino Uno
-
1 DC motor
-
1 Resistor 330Ω
-
1 Resistor 15Ω
-
1 AB USB cable
Source Code
// Connect motor on pin 2 in series with a 15 ohm resistor
// to limit the current to 40mA just not to overload the Arduino
const int motorPin = 2; const int buttonPin = 7; int buttonState = 0;
void setup () {pinMode (buttonPin, INPUT); pinMode (motorPin, OUTPUT);
}
void loop () {
buttonState = digitalRead (buttonPin); if (buttonState == HIGH) {
digitalWrite (motorPin, HIGH);
}
else {
digitalWrite (motorPin, LOW);
}
}
Tip
We can change the direction of rotation of a DC motor by simply reversing the direction of the current. With the same assembly of this lesson, the test reverses the connections of the motor and verify that it will start to rotate in the opposite direction.
LCD
The LCD is an important part of projects where you need to visualize the reading of a sensor or even to transmit information to the user.
In this exercise, you will learn how to connect the 2x16 LCD Display of your Kit, which already comes with the welded pins.
What Will You Learn?
-
Connect your LCD to Arduino Uno
-
Programming phrases to appear on the LCD
-
Adjust the brightness of the display with a potentiometer
-
Know the functions of the LiquidCrystal.h library
-
Use the functions:
-
LCD.print
-
LCD.setCursor
-
scrollDisplayLeft()
-
scrollDisplayRight()
Required Materials
-
1 Arduino Uno
-
1 LCD display
-
1 Potentiometer
-
1 AB USB cable Jumpers
-
1 Protoboard
Source Code
/ *
LiquidCrystal Multilogic Code Library
This library works with all displays compatible with the
Hitachi HD44780 driver.
Circuit:
* LCD RS pin on digital pin 12
* LCD pin Enable on digital pin 11
* LCD pin D4 pin on digital pin 5
* LCD pin D5 pin on digital pin 4
* LCD pin D6 pin on digital pin 3
* LCD pin D7 pin on digital pin 2
* LCD pin R / W on ground
* 10K Trimpot:
+ 5V on + 5V
* Earth on earth
* wiper to LCD VO pin (pin 3)
Public domain code based on the original lesson: http://www.arduino.cc/en/Lesson/LiquidCrystal
* /
// Includes library code: #include <LiquidCrystal.h>
// Initialize the library and define the pins used
LiquidCrystal lcd (12, 11, 5, 4, 3, 2);
void setup () {
// define the number of columns and lines of the display:
lcd begin (16, 2);
// Send the message to the display. lcd print ("Multilogic");
lcd setCursor (0.1); // Positions the cursor on the first column (0) and on the second line (1) of the Display.
lcd print ("shop");
}
void loop () {
}
Tips
If your project needs more space to view information or a different LCD, get to know these other options:
Exercise 1
The Arduino website offers several other projects with the
LiquidCrystal.h Library. Here we will do one more exercise using the same assembly of this Lesson.
In this exercise, you can also modify the original text and control how long your text stays fixed and how long it scrolls to the right or left.
/ *
LiquidCrystal Library - scrollDisplayLeft () and scrollDisplayRight ()
LiquidCrystal Multilogic Code Library
This library works with all displays compatible with the
Hitachi HD44780 driver
This code writes "Multilogic Shop" on the LCD and uses scrollDisplayLeft () and scrollDisplayRight () to pass the text.
Circuit:
* LCD RS pin on digital pin 12
* LCD pin Enable on digital pin 11
* LCD pin D4 pin on digital pin 5
* LCD pin D5 pin on digital pin 4
* LCD pin D6 pin on digital pin 3
* LCD pin D7 pin on digital pin 2
* LCD pin R / W on ground
* 10K Trimpot:
+ 5V on + 5V
* Earth on earth
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008 by David A. Mellis
library modified 5 Jul 2009 by Limor Fried (http://www.ladyada.net) example added 9 Jul 2009 by Tom Igoe
modified 22 Nov 2010 by Tom Igoe
Public domain code based on the original lesson: http://arduino.cc/en/Lesson/LiquidCrystalScroll
* /
// Includes library code:
#include <LiquidCrystal.h>
// Initialize the library and define the pins used
LiquidCrystal lcd (12, 11, 5, 4, 3, 2);
void setup () {
// define the number of columns and rows:
lcd begin (16, 2);
// Send the message to the display. lcd print ("Desired Text"); delay (2000);
}
void loop () {
// walks 16 positions for the text to exit the display on the left:
for (int positionCounter = 0; positionCounter <16; positionCounter ++) {
// walk a position to the left: lcd. scrollDisplayLeft ();
// Wait a moment: delay (250);
}
// walks 32 positions for the text to exit the display on the right:
for (int positionCounter = 0; positionCounter <32; positionCounter ++) {
// walk a position to the right: lcd. scrollDisplayRight ();
// Wait a moment: delay (250);
}
// walks 16 positions to the left to move back to the center: for (int positionCounter = 0; positionCounter <16; positionCounter ++) {
// walk a position to the left: lcd. scrollDisplayLeft ();
// Wait a moment: delay (250);
}
// delay at the end of the full loop:
delay (2000);
}
Typing into the LCD
Transmitting information or custom commands through the LCD can be useful and necessary in many projects or activities.
In this lesson, you will be able to control the text that appears on display through the Serial Monitor of the Arduino IDE in a fast and fun way.
What Will You Learn?
-
Connect your LCD to Arduino Uno
-
Customize phrases to appear in the LCD
-
Using the Serial Monitor to enter texts
Required Materials
-
1 Arduino Uno
-
1 LCD display
-
1 Potentiometer
-
1 AB USB cable Jumpers
-
1 Protoboard
Source Code
/ *
This code sends a text entered via USB to the Circuit display:
* LCD RS pin on digital pin 12
* LCD pin R / W on pin 11
* LCD pin Enable on digital pin 10
* LCD pin D4 pin on digital pin 5
* LCD pin D5 pin on digital pin 4
* LCD pin D6 pin on digital pin 3
* LCD pin D7 pin on digital pin 2
* 10K Trimpot:
+ 5V on + 5V
* Earth on earth
* wiper to LCD VO pin (pin 3)
* /
#include <LiquidCrystal.h>
LiquidCrystal lcd (12, 11, 10, 5, 4, 3, 2); void setup ()
{
Serial. begin (9600); lcd begin (2, 20);
lcd clear (); lcd setCursor (0,0); lcd print ("Multilogic"); lcd setCursor (0.1); lcd print ("shop");
}
void loop ()
{
if (Serial.available ())
{
// f reads through the serial monitor // char cr = Serial. read ();
// determine a character to clear the screen /// if (cr == '%')
{
lcd clear ();
}
// determine a character to jump to the bottom // else line if (cr = '>')
{
lcd.setCursor (0.1);
}
else
{
// if the typed character doesn't clear or skip line goes to display ///
// does not accept accent // lcd. write (cr);
}
}
}
Piezo as Analog Output
In this lesson, we will use a piezo as analog output. We use the possibility of processors to produce PWM signals to play music or emit sounds.
What Will I Learn?
Required Materials
-
1 Arduino Uno
-
1 Electric piezo
-
1 AB USB cable Jumpers
-
1 Protoboard
Tip
Do you want to amplify the sound of the piezo?
Place the back of the piezo (the whole golden part) on the bottom of a can of soda or chocolate can, and you will hear the loudest sound.
Use your creativity and test that other elements can amplify the sound of the piezo.
Scroll Bar with Processing
In this lesson, we will use Arduino's serial communication to interact with a Processing program running on the computer.
What Will You Learn?
From a graphical interface in Processing running on the computer, perform interactions with Arduino.
Previous Knowledge
Have the latest version of Processing installed and running on your computer.
Required Materials
Source Code
/ *
Scroll Bar with Processing
* /
char val; // variable to save the value received by the serial interface
char val_old; // variable to store the previous value received by the
serial interface
// setup function runs only once when program execution starts void setup () {
// we initialize the pins from 2 to 6 as pinMode (2, OUTPUT) outputs;
pinMode (3, OUTPUT);
pinMode (4, OUTPUT); pinMode (5, OUTPUT); pinMode (6, OUTPUT);
// we initialize the serial communication with a speed of 115200 baud Serial. begin (115200);
}
// the loop function is executed indefinitely
void loop () {
if (Serial. available ()) {// checks if data is available for reading
val = Serial. read (); // read the data and store it in val
}
if (val! = val_old) {// acts only if there is a change in the value of val
alloff (); // perform the alloff function that erases all LEDs for (int i = 2; i <(int (val) +2); i ++) {
// causes the value of i to vary from 2 to val + 2
// note that val stores a character and we need
// of the int (val) function to convert to a digitalWrite (i, HIGH) numerical value; // turns on the LED on pin i
}
val_old = val; // saves the value of val in val_old
}
}
void alloff () {// function to turn off all LEDs
for (int i = 2; i <7; i ++) {// causes i to vary from 2 to 6 digitalWrite (i, LOW); // turns off the LED on pin i
}
}
Arduino - Thermostat
In this lesson, we will set up a simple experimental thermostat with Arduino. This mounting causes a relay and an LED to turn on when the temperature drops below a set minimum value or turn off when it exceeds a set maximum temperature.
Note that temperature measurement is empirical and should not be
used as an accurate instrument.
What You Will learn?
-
Use your Arduino to control devices from the ambient temperature reading.
-
For this experiment, you can vary the temperature of the sensor just by holding it with your fingertips.
-
Note that just as we are using a relay to light an LED, you could drive any other device (a motor, for example) within the relay's power range.
Necessary Materials
Source Code
// Example of a simple experimental Arduino thermostat
// This setup makes the relay and a led
// turn on when the temperature drops below a minimum
// and turn off when it exceeds a maximum
// The temperature measurement is an empirical approximation
// Do not use an accurate instrument, but rather as a
didactic // thermostat model.
// relay connected to digital pin 2
// Led connected to digital pin 13
// resistive divider of thermistor connected to pin A0 analog
// declaration of variables:
#define pin_termistor A0
int rele = 2;
int led = 13;
float reading;
float read1;
// set acting mode:
void setup (void) {
pinMode (relay, OUTPUT);
pinMode (led, OUTPUT);
Serial.Begin (9600);
}
// infinite loop:
void loop (void) {
read = analogRead (pin_termistor);
Serial.print ("Read pin A0 =");
Serial.println (read);
read1 = (read * 0.2027) -85; // Calculate the temp. approximate
if (read1 <32) // set trigger temperature
{
digitalWrite (relay, HIGH); // relay
digitalWrite (led, HIGH); // turn on Led
}
if (read1> 35) // set cutoff point
{
digitalWrite (relay, LOW); // turn off
digitalWrite relay (led, LOW); // turn off Led
}
Serial.print ("Temperature approx. Celsius ="); // send and temp. for serial monitor
Serial.println (read1)
;
Serial.println ("");
delay (2500);
}