Intro To Arduino
Intro To Arduino
USB Jack
Example Code
int buttonPin = 3;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
}
void loop() {
// ...
}
loop()
After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops
consecutively, allowing your program to change and respond. Use it to actively control the Arduino board.
Example Code
int buttonPin = 3;
delay(1000);
}
digitalWrite()
• Write a HIGH or a LOW value to a digital pin.
• If the pin has been configured as an OUTPUT with pinMode(), its
voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V
boards) for HIGH, 0V (ground) for LOW.
Syntax
digitalWrite(pin, value)
Example Code (digitalWrite())
The code makes the digital pin 13 an OUTPUT and toggles it by alternating between HIGH
and LOW at one second pace.
void setup() {
pinMode(13, OUTPUT); // sets the digital pin 13 as output
}
void loop() {
digitalWrite(13, HIGH); // sets the digital pin 13 on
delay(1000); // waits for a second
digitalWrite(13, LOW); // sets the digital pin 13 off
delay(1000); // waits for a second
}
digitalRead()
Reads the value from a specified digital pin, either HIGH or LOW.
Syntax
digitalRead(pin)
Parameters
pin: the Arduino pin number you want to read
Returns
HIGH or LOW
Example Code (digitalRead())
Sets pin 13 to the same value as pin 7, declared as an input.
void setup() {
pinMode(ledPin, OUTPUT); // sets the digital pin 13 as output
pinMode(inPin, INPUT); // sets the digital pin 7 as input
}
void loop() {
val = digitalRead(inPin); // read the input pin
digitalWrite(ledPin, val); // sets the LED to the button's value
}
analogRead()
Reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog to
digital converter. This means that it will map input voltages between 0 and the operating voltage(5V or
3.3V) into integer values between 0 and 1023.
On an Arduino UNO, for example, this yields a resolution between readings of: 5 volts / 1024 units or,
0.0049 volts (4.9 mV) per unit.
Syntax
analogRead(pin)
Parameters
pin: the name of the analog input pin to read from (A0 to A5 on most boards, A0 to A6 on MKR boards,
A0 to A7 on the Mini and Nano, A0 to A15 on the Mega).
Returns
The analog reading on the pin. Although it is limited to the resolution of the analog to digital converter
(0-1023 for 10 bits or 0-4095 for 12 bits). Data type: int.
analogWrite()
Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to
analogWrite(), the pin will generate a steady rectangular wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or
digitalWrite()) on the same pin.
Syntax
analogWrite(pin, value)
Parameters
pin: the Arduino pin to write to. Allowed data types: int.
value: the duty cycle: between 0 (always off) and 255 (always on). Allowed data types: int.
Example Code
Sets the output to the LED proportional to the value read from the potentiometer.
void setup() {
pinMode(ledPin, OUTPUT); // sets the pin as output
}
void loop() {
val = analogRead(analogPin); // read the input pin
analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}
Variables
A variable is a place to store a piece of data. It has a name, a value, and a type.
For example, this statement (called a declaration):
creates a variable whose name is pin, whose value is 13, and whose type is int. Later on in
the program, you can refer to this variable by its name, at which point its value will be looked
up and used. For example, in this statement:
pinMode(pin, OUTPUT);
it is the value of pin (13) that will be passed to the pinMode() function. In this case, you don't
actually need to use a variable, this statement would work just as well:
pinMode(13, OUTPUT);
Variable Scope
It refers to the part of your program in which the variable can be used. This is determined by
where you declare it. For example, if you want to be able to use a variable anywhere in your
program, you can declare at the top of your code. This is called a global variable; here's an
example:
int pin = 13;
As you can see, pin is used in both the setup() and loop()
void setup() functions. Both functions are referring to the same
{ variable, so that changing it one will affect the value
pinMode(pin, OUTPUT);
}
void loop()
{
digitalWrite(pin, HIGH);
}
Variable Scope
int pin = 13; Here, the digitalWrite() function called from loop() will be
void setup() passed a value of 12, since that's the value that was assigned
{ to the variable in the setup() function.
pin = 12;
pinMode(pin, OUTPUT);
}
void loop()
{
digitalWrite(pin, HIGH);
}
Variable Scope
If you only need to use a variable in a In this case, the variable pin can only be used inside
single function, you can declare it there, the setup() function. If you try to do something like
in which case its scope will be limited to this:
that function. For example:
A potentiometer is a simple mechanical device that provides a varying amount of resistance when its
shaft is turned. By passing voltage through a potentiometer and into an analog input on your board, it
is possible to measure the amount of resistance produced by a potentiometer (or pot for short) as an
analog value. In this example you will monitor the state of your potentiometer after establishing serial
communication between your Arduino and your computer running the Arduino Software (IDE).
Hardware Required
•Arduino Board
•10k ohm Potentiometer
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the Serial Monitor.
Graphical representation is available using Serial Plotter (Tools > Serial
Plotter menu).
Attach the center pin of a potentiometer to pin A0, and the outside pins to
+5V and ground.
http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/
Serial.begin(9600);
Next, in the main loop of your code, you need to establish a variable to store the resistance value (which will be between 0 and 1023,
perfect for an int datatype) coming in from your potentiometer:
Finally, you need to print this information to your serial monitor window. You can do this with the command Serial.println() in your last
line of code:
Serial.println(sensorValue)
Now, when you open your Serial Monitor in the Arduino Software (IDE) (by clicking the icon that looks like a lens, on the right, in the
green top bar or using the keyboard shortcut Ctrl+Shift+M), you should see a steady stream of numbers ranging from 0-1023,
correlating to the position of the pot. As you turn your potentiometer, these numbers will respond almost instantly.
Printing Hello World
Turn on, LED light
int pin4 = 4,pin7 = 7, pin8 = 8;
void setup()
{
pinMode(pin4, OUTPUT);
pinMode(pin7, OUTPUT);
pinMode(pin8, OUTPUT);
void loop()
{
digitalWrite(pin4, HIGH);
digitalWrite(pin7, HIGH);
digitalWrite(pin8, HIGH);
delay(500);
digitalWrite(pin4, LOW);
digitalWrite(pin7, LOW);
digitalWrite(pin8, LOW);
delay(500);
}
Turn On, LED light with a Push Button
int inputStatus = 0;
void setup()
{
pinMode(13, OUTPUT);
pinMode(2, INPUT);
}
void loop()
{
inputStatus = digitalRead(2);
if (inputStatus == HIGH) {
digitalWrite(13, HIGH);
delay(100); // Wait for 100 millisecond(s)
}
else {
digitalWrite(13, LOW);
delay(100); // Wait for 100 millisecond(s)
}