Arduino Manual Student
Arduino Manual Student
Introduction
LEDs are small, powerful lights that are used in many different applications. To start off,
we will work on blinking an LED, the Hello World of microcontrollers. That’s right - it’s as
simple as turning a light on and off. It might not seem like much, but establishing this important
baseline will give you a solid foundation as we work toward more complex experiments.
Parts Needed
Arduino Uno R3
1x LED
1x 330Ω Resistor
2x Jumper Wires
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
Experiment 2:
A potentiometer is also known as a variable resistor. When powered with 5V, the middle
pin outputs a voltage between 0V and 5V, depending on the position of the knob on the
potentiometer. A potentiometer is a perfect demonstration of a variable voltage divider circuit.
The voltage is divided proportionate to the resistance between the middle pin and the ground pin.
In this circuit, you’ll learn how to use a potentiometer to control the brightness of an LED.
Parts Needed
Arduino Uno
1x LED
1x 330Ω Resistor
6x Jumper Wires
1x Potentiometer
void setup() // this function runs once when the sketch starts up
{
pinMode(ledPin, OUTPUT);
}
void loop() // this function runs repeatedly after setup() finishes
{
int sensorValue;
sensorValue = analogRead(sensorPin);
}
Experiment 3:
Introduction
You know what’s even more fun than a blinking LED? Changing colors with one LED.
RGB, or red-green-blue, LEDs have three different color-emitting diodes that can be combined
to create all sorts of colors. In this circuit, you’ll learn how to use an RGB LED to create unique
color combinations. Depending on how bright each diode is, nearly any color is possible!
Parts Needed
Arduino Uno
3x 330Ω Resistors
5x Jumper Wires
But which pin is which color? Pick up the RGB so that the longest pin (common ground)
is aligned to the left as shown in the graphic below. The pins are Red, Ground, Green,
and Blue – starting from the far left.
Circuit Diagram for Arduino
//Example sketch 03
const int RED_PIN = 9;
const int GREEN_PIN = 10;
const int BLUE_PIN = 11;
void setup()
{
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
}
void loop()
{
mainColors();
showSpectrum();
}
void mainColors()
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
delay(1000);
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
delay(1000);
}
void showSpectrum()
{
int x; // define an integer variable called "x"
{
showRGB(x); // Call RGBspectrum() with our new x
delay(DISPLAY_TIME); // Delay for 10 ms (1/100th of a second)
}
}
analogWrite(RED_PIN, redIntensity);
analogWrite(BLUE_PIN, blueIntensity);
analogWrite(GREEN_PIN, greenIntensity);
}
Experiment 4:
Introduction
Now that you’ve gotten your LED to blink on and off, it’s time to up the stakes a little bit
– by connecting eight LEDs at once. We’ll also give your Arduino R3 a little test by creating
various lighting sequences. This circuit is a great setup to start practicing writing your own
programs and getting a feel for the way Arduino works.
// Example sketch 04
void setup()
int index;
for(index = 0; index <= 7; index++)
pinMode(ledPins[index],OUTPUT);
void loop()
//oneAfterAnotherNoLoop()
void oneAfterAnotherNoLoop()
{
int delayTime = 100; // time (milliseconds) to pause between LEDs
void oneAfterAnotherLoop()
{
int index;
int delayTime = 100; // milliseconds to pause between LEDs
void oneOnAtATime()
{
int index;
int delayTime = 100; // milliseconds to pause between LEDs
void pingPong()
{
int index;
int delayTime = 100;
void marquee()
{
int index;
int delayTime = 200;
void randomLED()
{
int index;
int delayTime;
Introduction
Up until now, we’ve focused mostly on outputs. Now we’re going to go to the other end
of spectrum and play around with inputs. In experiment 2, we used an analog input to read the
potentiometer. In this circuit, we’ll be reading in one of the most common and simple inputs – a
push button – by using a digital input. The way a push button works with your Arduino Uno R3
is that when the button is pushed, the voltage goes LOW. Your Arduino Uno R3 reads this and
reacts accordingly.
In this circuit, you will also use a pull-up resistor, which keeps the voltage HIGH when
you’re not pressing the button.
Parts Needed
Arduino Uno
1x LED
1x 330Ω Resistor
7x Jumper Wires
2x Push Buttons
2x 10k Resistors
void setup()
{
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop()
{
int button1State, button2State; // variables to hold the pushbutton states
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
}
Experiment 6: Controlling the ON/OFF of an LED by Connecting photo resistor
Introduction
In experiment 2, you got to use a potentiometer, which varies resistance based on the
twisting of a knob. In this circuit, you’ll be using a photoresistor, which changes resistance based
on how much light the sensor receives. Since the Arduino Uno R3 can’t directly interpret
resistance (rather, it reads voltage), we need to use a voltage divider to use our photoresistor.
This voltage divider will output a high voltage when it is getting a lot of light and a low voltage
when little or no light is present.
Parts Needed
Arduino Uno
1x LED
1x 330Ω Resistor
6x Jumper Wires
1x Photoresistor
1x 10k Resistor
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
lightLevel = analogRead(sensorPin);
analogWrite(ledPin, lightLevel);
void manualTune()
{
void autoTune()
{
if (lightLevel < low)
{
low = lightLevel;
}
}
Experiment 7: Connecting temperature sensor and reading the output
Introduction
A temperature sensor is exactly what it sounds like – a sensor used to measure ambient
temperature. This particular sensor has three pins – a positive, a ground, and a signal. This is a
linear temperature sensor. A change in temperature of one degree centigrade is equal to a change
of 10 millivolts at the sensor output.
The TMP36 sensor has a nominal 750 mV at 25°C (about room temperature). In this
circuit, you’ll learn how to integrate the temperature sensor with your Arduino Uno R3, and use
the Arduino IDE’s serial monitor to display the temperature.
Parts Needed
Arduino Uno
5x Jumper Wires
1x Temperature Sensor
// Example sketch 07
void setup()
{
Serial.begin(9600);
void loop()
{
float voltage, degreesC, degreesF;
voltage = getVoltage(temperaturePin);
Serial.print("voltage: ");
Serial.print(voltage);
Serial.print(" deg C: ");
Serial.print(degreesC);
Serial.print(" deg F: ");
Serial.println(degreesF);
}
Experiment 8: Using PWM to control and rotate a servo
Introduction
Servos are ideal for embedded electronics applications because they do one thing very
well that motors cannot – they can move to a position accurately. By varying the pulse width of
the output voltage to a servo, you can move a servo to a specific position. For example, a pulse
of 1.5 milliseconds will move the servo 90 degrees. In this circuit, you’ll learn how to use PWM
(pulse width modulation) to control and rotate a servo.
Parts Needed
1x Arduino Uno
1x Servo
8x Jumper Wires
Servo myservo;
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
Experiment 9: Using flex/force sensor to control and rotate a servo
Introduction
In this circuit, we will use a flex sensor to measure, well, flex! A flex sensor uses carbon
on a strip of plastic to act like a variable resistor, but instead of changing the resistance by
turning a knob, you change it by flexing (bending) the component. We use a “voltage divider”
again to detect this change in resistance.
The sensor bends in one direction and the more it bends, the higher the resistance gets; it has a
range from about 10K ohm to 35K ohm. In this circuit we will use the amount of bend of the flex
sensor to control the position of a servo.
Parts Needed
1x Arduino Uno
1x Flex Sensor
1x Servo
1x 10k resistor
11x Jumper Wires
void setup()
{
servo1.attach(9);
}
void loop()
{
int position;
#include <Servo.h>
Servo servo1;
void setup()
{
Serial.begin(9600);
servo1.attach(9);
}
void loop()
{
int flexposition; // Input value from the analog pin.
int servoposition; // Output value to the servo.
flexposition = analogRead(flexpin);
servo1.write(servoposition);
Serial.print("sensor: ");
Serial.print(flexposition);
Serial.print(" servo: ");
Serial.println(servoposition);
Introduction
In this circuit, we are going to use yet another kind of variable resistor – this time, a soft
potentiometer (or soft pot). This is a thin and flexible strip that can detect where pressure is being
applied. By pressing down on various parts of the strip, you can vary the resistance from 100 to
10k ohms. You can use this ability to track movement on the soft pot, or simply as a button. In
this circuit, we’ll get the soft pot up and running to control an RGB LED.
Parts Needed
1x Arduino Uno
1x Soft Potentiometer
1x 10k resistor
3x 330Ω resistors
1x RGB LED
9x Jumper Wires
// Example sketch 10
const int RED_LED_PIN = 9; // Red LED Pin
const int GREEN_LED_PIN = 10; // Green LED Pin
const int BLUE_LED_PIN = 11; // Blue LED Pin
void setup()
{
void loop()
{
int sensorValue;
sensorValue = analogRead(SENSOR_PIN);
setRGB(sensorValue);
}
analogWrite(RED_LED_PIN, redValue);
analogWrite(GREEN_LED_PIN, greenValue);
analogWrite(BLUE_LED_PIN, blueValue);
}
Experiment 11: Switching ON/OFF a piezoelectric buzzer
Introduction
In this circuit, we’ll again bridge the gap between the digital world and the analog world.
We’ll be using a piezo buzzer that makes a small “click” when you apply voltage to it (try it!).
By itself that isn’t terribly exciting, but if you turn the voltage on and off hundreds of times a
second, the piezo buzzer will produce a tone. And if you string a bunch of tones together, you’ve
got music! This circuit and sketch will play a classic tune. We’ll never let you down!
Parts Needed
1x Arduino Uno
1x Piezo Buzzer
3x Jumper Wires
// Example sketch 11
void setup()
{
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
int i, duration;
while(true){}
int i;
const int numNotes = 8; // number of notes we're storing
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
Introduction
Back in experiment 8, you got to work with a servo motor. Now, we are going to tackle
spinning a motor. This requires the use of a transistor, which can switch a larger amount of
current than the Arduino Uno R3 can.
When using a transistor, you just need to make sure its maximum specs are high enough
for your use case. The transistor we are using for this circuit is rated at 40V max and 200
milliamps max – perfect for our toy motor! When the motor is spinning and suddenly turned off,
the magnetic field inside it collapses, generating a voltage spike. This can damage the transistor.
To prevent this, we use a “flyback diode”, which diverts the voltage spike around the transistor.
Parts Needed
1x Arduino Uno
1x Motor
1x 330Ω Resistor
1x NPN transistor
1x Diode 1N4148
6x Jumper Wires
void setup()
{
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
serialSpeed();
}
void motorOnThenOff()
{
int onTime = 3000; // milliseconds to turn the motor on
int offTime = 3000; // milliseconds to turn the motor off
void motorOnThenOffWithSpeed()
{
int Speed1 = 200; // between 0 (stopped) and 255 (full speed)
int Time1 = 3000; // milliseconds for speed 1
void serialSpeed()
{
int speed;
speed = Serial.parseInt();
analogWrite(motorPin, speed);
}
}
}
Experiment 13: Control ON/OFF of a relay
Introduction
In this circuit, we are going to use some of the lessons we learned in experiment 12 to
control a relay. A relay is basically an electrically controlled mechanical switch. Inside that
harmless looking plastic box is an electromagnet that, when it gets a jolt of energy, causes a
switch to trip. In this circuit, you’ll learn how to control a relay like a pro – giving your Arduino
even more powerful abilities!
Parts Needed
1x Arduino Uno
2x LEDs
2x 330Ω Resistors
1x Relay (SPDT)
1x Diode 1N4148
1x NPN Transistor
14x Jumper Wires
void setup() {
pinMode(relay, OUTPUT);
}
void loop() {
digitalWrite(relay, HIGH); // turn the relay on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(relay, LOW); // turn the relay off by making the voltage LOW
delay(1000); // wait for a second
}
Experiment 14: Using a shift register to control eight LEDs
Introduction
Now we are going to step into the world of ICs (integrated circuits). In this circuit, you’ll
learn all about using a shift register (also called a serial-to-parallel converter). The shift register
will give your RedBoard or Arduino Uno R3 an additional eight outputs, using only three pins on
your board. For this circuit, you’ll practice by using the shift register to control eight LEDs.
Parts Needed
1x Arduino Uno
8x LEDs
8x 330Ω Resistors
19x Jumper Wires
byte data = 0;
void setup()
{
pinMode(datapin, OUTPUT);
pinMode(clockpin, OUTPUT);
pinMode(latchpin, OUTPUT);
}
void loop()
{
bitWrite(data,desiredPin,desiredState);
digitalWrite(latchpin, HIGH);
digitalWrite(latchpin, LOW);
}
void oneAfterAnother()
{
int index;
int delayTime = 100; // Time (milliseconds) to pause between LEDs
// Make this smaller for faster switching
for(index = 0; index <= 7; index++)
{
shiftWrite(index, HIGH);
delay(delayTime);
}
void oneOnAtATime()
{
int index;
int delayTime = 100; // Time (milliseconds) to pause between LEDs
void pingPong()
{
int index;
int delayTime = 100; // time (milliseconds) to pause between LEDs
// make this smaller for faster switching
void randomLED()
{
int index;
int delayTime = 100; // time (milliseconds) to pause between LEDs
// make this smaller for faster switching
void marquee()
{
int index;
int delayTime = 200; // Time (milliseconds) to pause between LEDs
void binaryCount()
{
int delayTime = 1000; // time (milliseconds) to pause between LEDs
digitalWrite(latchpin, HIGH);
digitalWrite(latchpin, LOW);
data++;
delay(delayTime);
}
15. Using ultrasonic sensor module to activate buzzer:
Parts Needed
Arduino UNO R3
Half Breadboard
#define trigPin 13
#define echoPin 12
#define led 11
#define led2 10
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
}
void loop() {
long duration, distance;
digitalWrite(trigPin, LOW); // Added this line
delayMicroseconds(2); // Added this line
digitalWrite(trigPin, HIGH);
In this tutorial you will learn how to use an FSR - Force Sensitive Resistor with Arduino
to fade an LED. This sensor is a variable resistor just like a photocell or flex sensor. The
resistance changes by applying pressure on it.
Arduino uno
Breadboard
LED
void loop(){
}
17. Detecting vibration and knocks using sound and piezomodule
Arduino uno
Buzzer
Vibration/shake sensor
The connections are pretty easy, see the above image with the breadboard circuit schematic.
Connect one pin of vibration sensor to Arduino Analog pin A0 and the other to 5V pin.
Now connect the buzzer, one pin to Arduino pin 8 and the other to GND.
void setup()
{
Serial.begin(9600); //Only for debugging
pinMode(buzzer, OUTPUT);
}
void loop()
{
sensor = analogRead(A0);
//While sensor is not moving, analog pin receive 1023~1024 value
if (sensor<1022){
tone(buzzer, 500);
Serial.print("Sensor Value: ");
Serial.println(sensor);
}
else{
noTone(buzzer);
Serial.print("Sensor Value: ");
Serial.println(sensor);
}
As the datasheet says, ADXL335 is a small, thin, low power, complete 3-axis accelero-meter
with signal conditioned voltage outputs. The product measures acceleration with a minimum
full-scale range of ±3 g. It can measure the static acceleration of gravity in tilt-sensing
applications, as well as dynamic acceleration resulting from motion, shock, or vibration.
ADXL335 is 3v3 compatible device, it's powered by a 3.3v source and also generates 3.3v peak
outputs. It has three outputs for each axis i.e. X, Y & Z. These are analog outputs and thus
require an ADC in a micro-controller. Arduino solves this problem. We will be using the analog
functions of Arduino.
2. ADXL335 accelerometer
3. Connecting wires(Relimates)
int sv1 = 0;
int ov1 = 0;
int sv2 = 0;
int ov2= 0;
int sv3 = 0;
int ov3= 0;
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
void loop() {
analogReference(EXTERNAL); //connect 3.3v to AREF
// read the analog in value:
sv1 = analogRead(ap1);
// map it to the range of the analog out:
ov1 = map(sv1, 0, 1023, 0, 255);
// change the analog out value:
delay(2);
//
sv2 = analogRead(ap2);
Serial.print("Ysensor2 = " );
Serial.print(sv2);
Serial.print("\t output2 = ");
Serial.println(ov2);
Serial.print("Zsensor3 = " );
Serial.print(sv3);
Serial.print("\t output3 = ");
Serial.println(ov3);
delay(3000);