0% found this document useful (0 votes)
14 views

Activity 1

The document provides instructions for an activity introducing the Arduino IDE and hardware. It explains the basic code structure needed to compile a sketch and provides examples of blinking an LED and reading a switch. Circuit diagrams and code are given for each example to get started with physical input and output using an Arduino board.

Uploaded by

electromagnetism
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Activity 1

The document provides instructions for an activity introducing the Arduino IDE and hardware. It explains the basic code structure needed to compile a sketch and provides examples of blinking an LED and reading a switch. Circuit diagrams and code are given for each example to get started with physical input and output using an Arduino board.

Uploaded by

electromagnetism
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Activity 1

Learning Objectives:
1. Introduction to the Arduino IDE
2. Knowledge of associated hardware and peripherals
3. Understand of hardware and software integration

Bare Minimum code needed to get started


This example contains the bare minimum of code you need for an Arduino sketch to compile:
the setup() method and the loop() method.

Hardware Required
Arduino Board

Circuit
Only your Arduino Board is needed for this example.

Code
The setup() function is called when a sketch starts. Use it to initialize variables, pin modes, start
using libraries, etc. The setup function will only run once, after each powerup or reset of the
Arduino board.

After creating a setup() function, the loop() function does precisely what its name suggests, and
loops consecutively, allowing your program to change and respond as it runs. Code in the loop()
section of your sketch is used to actively control the Arduino board.
The code below won't actually do anything, but it's structure is useful for copying and pasting to
get you started on any sketch of your own. It also shows you how to make comments in your
code.

Any line that starts with two slashes (//) will not be read by the compiler, so you can write
anything you want after it. Commenting your code like this can be particularly helpful in
explaining, both to yourself and others, how your program functions step by step.

void setup() {
// put your setup code here, to run once:

void loop() {
// put your main code here, to run repeatedly:

Blink
This example shows the simplest thing you can do with an Arduino to see physical output: it
blinks an LED.

Hardware Required

Arduino Board
LED
Resistor, anything between 220 ohm to 1K ohm

Circuit
To build the circuit, connect one end of the resistor to Arduino pin 13. Connect the long leg of
the LED (the positive leg, called the anode) to the other end of the resistor. Connect the short
leg of the LED (the negative leg, called the cathode) to the Arduino GND, as shown in the
diagram and the schematic below.

Most Arduino boards already have an LED attached to pin 13 on the board itself. If you run this
example with no hardware attached, you should see that LED blink.
Schematic

After you build the circuit plug your Arduino board into your computer, start the Arduino IDE, and
enter the code below.

Code
In the program below, the first thing you do is to initialize pin 13 as an output pin with the line

pinMode(13, OUTPUT);

In the main loop, you turn the LED on with the line:

digitalWrite(13, HIGH);

This supplies 5 volts to pin 13. That creates a voltage difference across the pins of the LED, and
lights it up. Then you turn it off with the line:

digitalWrite(13, LOW);

That takes pin 13 back to 0 volts, and turns the LED off. In between the on and the off, you want
enough time for a person to see the change, so the delay() commands tell the Arduino to do
nothing for 1000 milliseconds, or one second. When you use the delay() command, nothing else
happens for that amount of time. Once you've understood the basic examples, check out the
BlinkWithoutDelay example to learn how to create a delay while doing other things.

Once you've understood this example, check out the DigitalReadSerial example to learn how
read a switch connected to the Arduino.
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.

This example code is in the public domain.


*/

// Pin 13 has an LED connected on most Arduino boards.


// give it a name:
int led = 13;

// the setup routine runs once when you press reset:


void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:


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
}

Blink Without Delay


Sometimes you need to do two things at once. For example you might want to blink an LED
while reading a button press. In this case, you don't want to use delay(), because Arduino
pauses your program during the delay(). If the button is pressed while Arduino is paused waiting
for the delay() to pass, your program will miss the button press.

This sketch demonstrates how to blink an LED without using delay(). It turns on the LED on and
then makes note of the time. Then, each time through loop(), it checks to see if the desired blink
time has passed. If it has, it toggles the LED on or off and makes note of the new time. In this
way the LED blinks continuously.

An analogy would be warming up a pizza in your microwave, and also waiting some important
email. You put the pizza in the microwave and set it for 10 minutes. The analogy to using
delay() would be to sit in front of the microwave watching the timer count down from 10 minutes
until the timer reaches zero. If the important email arrives during this time you will miss it.

What you would do in real life would be to turn on the pizza, and then check your email, and
then maybe do something else (that doesn't take too long!) and every so often you will come
back to the microwave to see if the timer has reached zero, indicating that your pizza is done.
In this tutorial you will learn how to set up a similar timer.

Hardware Required
Arduino Board
LED
Resistor, anything between 220 ohm to 1K ohm

Circuit

To build the circuit, connect one end of the resistor to Arduino pin 13. Connect the long leg of
the LED (the positive leg, called the anode) to the other end of the resistor. Connect the short
leg of the LED (the negative leg, called the cathode) to the Arduino GND, as shown in the
diagram above and the schematic below.

Most Arduino boards already have an LED attached to pin 13 on the board itself. If you run this
example with no hardware attached, you should see that LED blink.

Schematic:
After you build the circuit plug your Arduino board into your computer, start the Arduino IDE, and
enter the code below.

Code
The code below uses the millis() function, a command that returns the number of milliseconds
since the Arduino board started running its current program, to blink an LED.

/* Blink without Delay

Turns on and off a light emitting diode(LED) connected to a digital


pin, without using the delay() function. This means that other code
can run at the same time without being interrupted by the LED code.

The circuit:
* LED attached from pin 13 to ground.
* Note: on most Arduinos, there is already an LED on the board
that's attached to pin 13, so no hardware is needed for this example.

*/

// constants won't change. Used here to


// set pin numbers:
const int ledPin = 13; // the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,


// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to blink (milliseconds)

void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}

void loop()
{
// here is where you'd put code that needs to be running all the time.

// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();

if(currentMillis - previousMillis > interval) {


// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:


if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

// set the LED with the ledState of the variable:


digitalWrite(ledPin, ledState);
}
}
Digital Read Serial
This example shows you how to monitor the state of a switch by establishing serial
communication between your Arduino and your computer over USB.

Hardware Required

Arduino Board
A momentary switch, button, or toggle switch
10k ohm resistor
breadboard
hook-up wire

Circuit

Connect three wires to the Arduino board. The first two, red and black, connect to the two long
vertical rows on the side of the breadboard to provide access to the 5 volt supply and ground.
The third wire goes from digital pin 2 to one leg of the pushbutton. That same leg of the button
connects through a pull-down resistor (here 10 KOhms) to ground. The other leg of the button
connects to the 5 volt supply.

Pushbuttons or switches connect two points in a circuit when you press them. When the
pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton,
so the pin is connected to ground (through the pull-down resistor) and reads as LOW, or 0.
When the button is closed (pressed), it makes a connection between its two legs, connecting
the pin to 5 volts, so that the pin reads as HIGH, or 1.

If you disconnect the digital i/o pin from everything, the LED may blink erratically. This is
because the input is "floating" - that is, it doesn't have a solid connection to voltage or ground,
and it will randomly return either HIGH or LOW. That's why you need a pull-down resistor in the
circuit.

Schematic

Code
In the program below, the very first thing that you do will in the setup function is to begin serial
communications, at 9600 bits of data per second, between your Arduino and your computer with
the line:

Serial.begin(9600);
Next, initialize digital pin 2, the pin that will read the output from your button, as an input:

pinMode(2,INPUT);

Now that your setup has been completed, move into the main loop of your code. When your
button is pressed, 5 volts will freely flow through your circuit, and when it is not pressed, the
input pin will be connected to ground through the 10-kilohm resistor. This is a digital input,
meaning that the switch can only be in either an on state (seen by your Arduino as a "1", or
HIGH) or an off state (seen by your Arduino as a "0", or LOW), with nothing in between.

The first thing you need to do in the main loop of your program is to establish a variable to hold
the information coming in from your switch. Since the information coming in from the switch will
be either a "1" or a "0", you can use an int datatype. Call this variable sensorValue, and set it to
equal whatever is being read on digital pin 2. You can accomplish all this with just one line of
code:

int sensorValue = digitalRead(2);

Once the Arduino has read the input, make it print this information back to the computer as a
decimal value. You can do this with the command Serial.println() in our last line of code:

Serial.println(sensorValue);

Now, when you open your Serial Monitor in the Arduino environment, you will see a stream of
"0"s if your switch is open, or "1"s if your switch is closed.
/*
DigitalReadSerial
Reads a digital input on pin 2, prints the result to the serial monitor

This example code is in the public domain.


*/

// digital pin 2 has a pushbutton attached to it. Give it a name:


int pushButton = 2;

// the setup routine runs once when you press reset:


void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the pushbutton's pin an input:
pinMode(pushButton, INPUT);
}

// the loop routine runs over and over again forever:


void loop() {
// read the input pin:
int buttonState = digitalRead(pushButton);
// print out the state of the button:
Serial.println(buttonState);
delay(1); // delay in between reads for stability
}

You might also like