Sie sind auf Seite 1von 38

BitStoc Electronics

TUTORIAL / USER MANUAL www.bitstoc.com

ARDUINO STARTER KIT Manual

The Arduino Starter Kit is the primary tool we use to start learning Electronics and Programming. The
kit includes the main microcontroller board the Arduino UNO R3 based on ATmega328. It has 14 digital
input/output pins (6 of which can be PWM outputs), 6 analog inputs, a 16 MHz crystal oscillator, a USB
connection, a power jack, an ISP header, and a reset button.

To Get Started, follow the course and examples

SPECIFICATION and FEATURES

Microcontroller = ATmega328
Operating Voltage = 5V
Input Voltage (recommended) = 7-12V

WWW.BITSTOC.COM BitStoc Electronics Page 1


Digital I/O Pins = 14 (of which 6 provide PWM output)
Analog Input Pins = 6
Clock Speed = 16 MHz
PREPARE THE SOFTWARE and HARDWARE

1. Download the Arduino IDE (Integrated Development Environment) by opening your Internet
browser and going to https://www.arduino.cc/en/Main/Software

By default we use Windows Operating System, Click the link on the red box. After downloading
locate the file and place it in your preferred computer disk storage.

If you are having trouble downloading and installing the hardware and software, go to
https://www.arduino.cc/en/Guide/Windows for a step by step guide.

2. Connect your Arduino board to your computer using the included USB cable. When you connect
your Arduino board to your computer, it automatically installs the Arduino DRIVERS and the board is
recognized as Arduino UNO board by your computer.

WWW.BITSTOC.COM BitStoc Electronics Page 2


If you are having trouble downloading and installing the hardware and software, go to
https://www.arduino.cc/en/Guide/Windows for a step by step guide.
HARDWARE OVERVIEW

Arduino UNO R3 (Main Microcontroller Board)

WWW.BITSTOC.COM BitStoc Electronics Page 3


Arduino UNO R3 board and Breadboard for prototyping
3. Open the Arduino IDE by opening the downloaded file folder earlier in step 1. Click the
arduino.exe file and the Arduino IDE programming environment will pop out.

4. Now our Software and Hardware is ready, we prepare the Starter Kit Materials.

WWW.BITSTOC.COM BitStoc Electronics Page 4


Arduino Starter Kit Parts List
1x Arduino Uno R3
1x USB Cable
1x Servo Motor SG-90 1x
30pcs Jumper Wires
1x Piezo Buzzer
1x LDR (Light Dependent Resistor) Photocell
1x Breadboard Standard Solderless
4x LED (5mm) Green
4x LED (5mm) Red
1x LED (5mm) RGB Common Cathode
2x Push Button Switch
10x 330 Ohms Resistor
5x 10k Ohms Resistor
5x 1k Ohms Resistor
1x 10k Ohms Potentiomaeter
1x Diode (1N4001)
1x Temperature Sensor LM35
1x DC Motor
1x Transistor BC547 NPN

Project #1: LED Blink and DIGITAL Output________________________

WWW.BITSTOC.COM BitStoc Electronics Page 5


Fig. 1.1 Fig. 1.2

Materials Needed:
1x Arduino UNO board
1x Breadboard (half-size)
1x Red LED
1x 330 ohm Resistor
Connecting wires

Construct the circuit with correct connections and part orientation by following the graphical
representation shown in Fig. 1.1. Your actual circuit will look like in Fig. 1.2.

UPLOADING the Arduino Code


(this step applies to all Arduino Code upload that we do in each of the next projects and for other
applications)

After assembly, copy the Arduino code from Sample Code 1.1 found in the next part and paste it in
the Arduino IDE.

Sample Code 1.1


/*
Blink and DIGITAL Output
Turns on an LED on for one second, then off for one second, repeatedly.
*/

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
}

// the loop function runs over and over again forever


void loop() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

STEPS TO UPLOAD CODE to the Arduino board

WWW.BITSTOC.COM BitStoc Electronics Page 6


*To Uploade the code from the Arduino IDE to the Arduino Board, we first need to compile the code by
following the steps.

1) Click on the Compile Button.


2) Wait until compiling is finish and
you should see a Done Compiling
sign.

*Choose the correct board where we will upload the Arduino Code. In
the Arduino IDE go to Tools > Board > click Arduino UNO.

WWW.BITSTOC.COM BitStoc Electronics Page 7


*Next, choose the port where the Arduino Board is detected. In this case our Arduino Board is assigned to
Port COM14. *Every Arduino board connected to the computer will be assigned with its own designated
COM port number, remember this COM port number for all your code uploads.

WWW.BITSTOC.COM BitStoc Electronics Page 8


* Click on the Upload Button.
* Wait until uploading is finish and
you should see a Done uploading
sign.

The Output

After successfully Uploading the code to the Arduino board, you should see the LED blinking ON and OFF
repeatedly every 1 second (1000ms).

Other things to do

Make the LED blink faster or slower by changing the codes delay() value. Change the delay(1000) to
delay(200) to make the LED blink faster or any value you desire to make the blink faster or slower.

WWW.BITSTOC.COM BitStoc Electronics Page 9


Project # :
2 Push Button and DIGITAL Input_______________________

Fig. 2.1 Fig. 2.2

Materials Needed:
1x Arduino UNO board 2x 10k Ohm Resistor
1x Breadboard (half-size) 1x 330 ohm Resistor
2x Push Button Switch Connecting wires
1x Red LED

Working the Circuit

Construct the circuit with correct connections and part orientation by following the graphical
representation shown in Fig. 2.1. Your actual circuit will look like in Fig. 2.2.

WWW.BITSTOC.COM BitStoc Electronics Page 10


Project # :
After assembly, copy the Arduino code from Sample Code 2.1 found in the next part and paste it in
the Arduino IDE. Complie and Upload the code to your Arduino board by following the same steps we
did in Project number 1 with STEPS TO UPLOAD CODE to the Arduino board.

2.1

/*
Buttons and DIGITAL Inputs
*/

// assign Arduino Pins for the LED and Buttons


int ledRedPin = 3;
int buttonLeftPin = 9;
int buttonRightPin = 8;

void setup() {
pinMode(ledRedPin, OUTPUT); // set Arduino pin 3 as output for LED
pinMode(buttonLeftPin, INPUT); // set Left button as a Digital Input
pinMode(buttonRightPin, INPUT); // set Right button as a Digital Input
}
void loop() {
if (digitalRead(buttonLeftPin) == LOW) // if Left Button is Pressed
{
digitalWrite(ledRedPin, HIGH); // turn ON the LED
}
if (digitalRead(buttonRightPin) == LOW) // if Right Button is Pressed
{
digitalWrite(ledRedPin, LOW); // tun OFF the LED
}
}

The Output

The Red LED should be OFF when the circuit starts after uploading the code. Now Press the Left Button
on the breadboard and you should see the Red LED light up (ON). Now Press the Right Button and the
Red LED will turn OFF.

WWW.BITSTOC.COM BitStoc Electronics Page 11


Sample Code

Other things to do

Add additional LEDs and Buttons to create a Push Button controlled Traffic Light Switch.

3 RGB LED and Analog Outputs_________________________

Fig. 3.1 Fig. 3.2

WWW.BITSTOC.COM BitStoc Electronics Page 12


Project # :
Materials Needed:
1x Arduino UNO board
1x Breadboard (half-size)
1x RGB LED
3x 330 ohm Resistor
Connecting wires

Working the Circuit

Construct the circuit with correct connections and part orientation by following the graphical
representation shown in Fig. 3.1. Your actual circuit will look like in Fig. 3.2.

After assembly, copy the Arduino code fro m Sample Code 3.1 found in the next part and paste it in
the Arduino IDE. Complie and Upload the code to your Arduino board by following the same steps
we did in Project number 1 with STEPS TO UPLOAD CODE to the Arduino board.

WWW.BITSTOC.COM BitStoc Electronics Page 13


Sample Code
3.1

/*
RGB LED and Analog Outputs
*/

int redPin = 11;


int greenPin = 10;
int bluePin = 9;

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

void loop()
{
setColor(255, 0, 0); // red
delay(1000);
setColor(0, 255, 0); // green
delay(1000);
setColor(0, 0, 255); // blue
delay(1000);
setColor(255, 255, 0); // yellow
delay(1000);
setColor(80, 0, 80); // purple
delay(1000);
setColor(255, 255, 255); // white
delay(1000);
}

void setColor(int red, int green, int blue)


{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}

The Output

The RGB LED will change colors every second following the sequence Red, Green, Blue, Yellow, Purple,
White as defined in the Sample Code.

WWW.BITSTOC.COM BitStoc Electronics Page 14


Project # :
Other things to do
Change the values from the 3 parameters in the setColor() function to play with different colors.
4 Potentiometer, Analog Inputs, Serial Monitor _

Fig. 4.1 Fig. 4.2

Materials Needed:
1x Arduino UNO board
1x Breadboard (half-size)
1x 10k Ohm Potentiometer
Connecting wires

Working the Circuit

WWW.BITSTOC.COM BitStoc Electronics Page 15


Sample Code
Construct the circuit with correct connections and part orientation by following the graphical
representation shown in Fig. 4.1. Your actual circuit will look like in Fig. 4.2.
After assembly, copy the Arduino code from Sample Code 4.1 found in the next part and paste it in
the Arduino IDE. Compile and Upload the code to your Arduino board by following the same steps
we did in Project number 1 with STEPS TO UPLOAD CODE to the Arduino board.

4.1

/*
Potentiometer and Analog Inputs
*/

// assign Arduino Pin for reading the Analog Voltage


// declare variable readInput for the output reading from 0 - 1023
int potentiometerPin = 0;
int readInput = 0;

void setup(){
Serial.begin(9600); // set baudrate to use in the Serial Monitor
}

void loop(){
// analogRead translates the potentiometer voltage from GND 0V to VCC 5V
// into 0 to 1023 and assign it to readInput variable
readInput = analogRead(potentiometerPin);

// print the value to the Serial Monitor to see the reading


Serial.println(readInput);
delay(200);
}

The Output

We will use a built in application included in Arduino IDE, the Serial Monitor. It is used to emulate a
communication channel for sending and receiving datas back and forth from an electronic device
connected to a computer. In this part, we will see the value of the potentiometer reading display in the
Serial Monitor.

After uploading the code Open the Serial Monitor by clicking the button shown below.

WWW.BITSTOC.COM BitStoc Electronics Page 16


Project # :

WWW.BITSTOC.COM BitStoc Electronics Page 17


The Serial Monitor application will show up and we will see the initial values show up.
Turn the knob of the potentiometer left to right and vice versa and we should see the value go from high
to low and vice versa.

WWW.BITSTOC.COM BitStoc Electronics Page 18


Project #5: LDR or Photocell/Photoresistor (light sensor) _

Fig. 5.1 Fig. 5.2

Materials Needed:
1x Arduino UNO board 1x Red LED
1x Breadboard (half-size) 1x 330 ohm resistor (orange-orange-brown)
1x LDR (Photocell/Photoresistor) Connecting wires
1x 10k ohm resistor (brown-black-orange)

WWW.BITSTOC.COM BitStoc Electronics Page 19


Construct the circuit with correct connections and part orientation by following the graphical
representation shown in Fig. 5.1. Your actual circuit will look like in Fig. 5.2.

Copy the Arduino code from Sample Code 5.1 and paste it in the Arduino IDE. Compile and Upload
the code to your Arduino board by following the same steps we did in Project number 1.

WWW.BITSTOC.COM BitStoc Electronics Page 20


Sample Code 5.1
/*
LDR or Photocell/Photoresistor (light sensor)
*/

//assign variables we use for the program


int lightPin = 0; //pin for the LDR Photo resistor
int ledPin = 12; //pin for LED
int lightValue = 0; //variable to assign for the value of the light represented by the LDR

// the setting up of pins.


void setup()
{
Serial.begin(9600); //Begin serial communcation
pinMode(ledPin, OUTPUT); // initialize the "ledPin (arduino pin 12) as an output.
}

// the loop is where your program runs repeatedly.


void loop()
{
// Read the value from the analog pin "lightPin" where the LDR is connected
// and print to the Serial monitor
lightValue = analogRead(lightPin);
Serial.println(lightValue);

// The reading will not reach 0 or 1024, it will be from around 16 (brightest) and 970 (darkest)
// because of the internal resistance of the resistor and LDR.
// Compare light and dark conditions to turn ON and OFF the LED
if (lightValue > 500) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}

delay(10); //short delay for faster response to light.


}

The Output

Upload the program to your Arduino board and open the Serial Monitor the same step from the previous
project. You will see the values changing (shown in the image below) as you move the circuit with the LDR
on dark and bright light. You will also see the red LED turn ON when it starts to get darker and it will turn
OFF when the environment get brighter lights.

WWW.BITSTOC.COM BitStoc Electronics Page 21


Example output of values detected by the LDR. When the value is low this mean the LDR is exposed to a
bright light. When it is high, the LDR is exposed to a darker environment. We set in the program that
around the value of 500 which is mid dark/bright we make the LED turn ON and OFF.

Other things to do

Change the value in the programs if condition to a lower or higher value and you will have an automatic
light detector for bright and dark environment of your own place!

WWW.BITSTOC.COM BitStoc Electronics Page 22


Project #6: Buzzer (making sounds) _

Fig. 6.1 Fig. 6.2

Materials Needed:
1x Arduino UNO board
1x Breadboard (half-size)
1x Buzzer (passive type)
Connecting wires

WWW.BITSTOC.COM BitStoc Electronics Page 23


Construct the circuit with correct connections and part orientation by following the graphical
representation shown in Fig. 6.1. Your actual circuit will look like in Fig. 6.2.

Copy the Arduino code from Sample Code 6.1 and paste it in the Arduino IDE. Compile and Upload
the code to your Arduino board by following the same steps we did in Project number 1.

Sample Code 6.1


/*
Project 6 - Buzzer (making sounds)
*/

//assign variables to use in our program


int buzzerPin = 8; //Arduino pin to connect the positive pin of buzzer
int listTones = 10; //number of tones we will play

//list of tones we will play which corresponds to


// C, C#, D, D#, E, F, F#, G, G#, A
int tones[] = {261, 277, 294, 311, 330, 349, 370, 392, 415, 440};

void setup() {
//we do not need to put any code initialization here.
}

void loop() {
// A "for" loop function is used to make a count from 0 to 9 using a variable "i".
// This "i" corresponds to the tone sequences listed like
// for i = 0 is for 261 and for i = 9 is for 440.
for (int i = 0; i < listTones; i++)
{
// The "tone" command from the Arduino library need only 2 parameters
// first parameter is the pin for the buzzer (buzzerPin)
// and second the tones we wish to play (tones)
tone(buzzerPin, tones[i]);
delay(500);
}

noTone(buzzerPin); // call the "noTone" command to stop playing any tones


delay(1000); // delay 1 second before playing again

WWW.BITSTOC.COM BitStoc Electronics Page 24


The Output

The program starts to play the tones that we put in the tone[] array. 10 tones will be played
simultaneously with a 1 second delay after the last tone and the program plays back the first tone.

Other things to do

Add some other tone values to play different musical tones. You may also connect the LDR from the
previous project and program the Arduino that equivalent values read by the LDR will be played on the
speaker!
Project #7: Temperature Sensing _

Fig. 7.1 Fig. 7.2

WWW.BITSTOC.COM BitStoc Electronics Page 25


Materials Needed:
1x Arduino UNO board
1x Breadboard (half-size)
1x LM35 temperature sensor IC
Connecting Wires

Construct the circuit with correct connections and part orientation by following the graphical
representation shown in Fig. 7.1. Your actual circuit will look like in Fig. 7.2.

Copy the Arduino code from Sample Code 7.1 and paste it in the Arduino IDE. Compile and Upload the
code to your Arduino board by following the same steps.

Sample Code 7.1

/*
Project 7 - Temperature Sensing with LM35
*/

//assign variable to use for the program float tempReading; // use a float
variable tempReading for decimal reading
float correctTemperature; // use a float variable correctTemperature for decimal reading int
temperaturesensorPin = 0; // use Arduino Analog pin 0 for temprature reading

void setup(){

Serial.begin(9600); // call a serial monitor communication to view the readings


}
// the loop is where your program runs repeatedly.
void loop() {
// First we get the value of the temperature read by the sensor
// from Arduino analog pin 0.
tempReading = analogRead(temperaturesensorPin);

// We multiply the resulting value of "tempreading" by .488


// to get the exact callibrated value of the temperature in degree Celsius.
correctTemperature = tempReading * 0.488;

WWW.BITSTOC.COM BitStoc Electronics Page 26


Serial.print(correctTemperature); // print the result to the serial monitor.
Serial.println(" degree Celsius"); // print result in a new line
delay(1000); // 1 second delay to display the value every second.
}

The Output

Run the program and open the Serial Monitor. You should see the values of the temperature in degrees
Celsius print out in the Serial monitor. From the code above, the value will be printed every 1 second.

Other things to do

Put your circuit board near a fan or air condition unit and you should see that the value will slowly go
down since the temperature sensor detects a colder environment. You should see in the serial monitor
some data the same as in the image below.

Example output values from the temperature sensor IC, you will see a decrease in value when you point
the temperature sensor near a fan or air condition unit or any cold places.

WWW.BITSTOC.COM BitStoc Electronics Page 27


Project #8: LCD Display _
(optional item inclusion in Starter Kit)

WWW.BITSTOC.COM BitStoc Electronics Page 28


Fig. 8.1 Fig. 8.2

LCD product link: http://www.bitstoc.com/index.php?route=product/product&product_id=60

Materials Needed:
1x Arduino UNO board
1x Breadboard (half-size)
1x 330 ohm resistor (orange-orange-brown)
1x 10k potentiometer
1X LCD module 16x2 character
Connecting wires

Construct the circuit with correct connections and part orientation by following the graphical
representation shown in Fig. 8.1. Your actual circuit will look like in Fig. 8.2.
Copy the Arduino code from Sample Code 8.1 and paste it in the Arduino IDE. Compile and Upload the
code to your Arduino board by following the same steps we did in Project number 1.

THE CIRCUIT CONNECTION

LCD pin 1 (VSS) to GND LCD


pin 2 (VDD) to 5V+

WWW.BITSTOC.COM BitStoc Electronics Page 29


LCD pin 3 (V0) to Center of potentiometer
LCD pin 4 (RS) to Arduino pin 12
LCD pin 5 (RW) to GND
LCD pin 6 (E/Enable) to Arduino pin 11
LCD pin 7-10 (D0-D3) no connection
LCD pin 11 (D4) to Arduino pin 5
LCD pin 12 (D5) to Arduino pin 4
LCD pin 13 (D6) to Arduino pin 3
LCD pin 14 (D7) to Arduino pin 2
LCD pin 15 (A,Anode) to 300 ohm resistor to 5V+
LCD pin 16 (K,Cathode) to GND
*one side of potentiometer to 5V+ and one side to GND

Sample Schematic

WWW.BITSTOC.COM BitStoc Electronics Page 30


Sample Code 8.1
/*
Project 8 - LCD Display
*/

// We use the "#include" command to use an LCD library "LiquidCrystal.h"


// Always include this line when using an LCD display
#include <LiquidCrystal.h>

// An LCD needs some initialization pins, from the Arduino board


// we use the pins listed in the "lcd()" function below
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
// To start using the LCD, we first need to set up the LCD's number of columns and rows:
// thus, put (16 columns and 2 rows) using the "lcd.begin(x,y)" command
lcd.begin(16, 2);
// Now, we can print a message to the LCD.
lcd.print("Hello, World!");
}

void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// We test out by printing the number of seconds since the time we start or reset the Arduino
lcd.print(millis() / 1000);
}

The Output

Compile and Upload the program. Start and you will see on the first line a text Hello, World! and on the
second line is an increasing count of number in seconds from which you starter or reset the Arduino board.

Other things to do

Try changing the text on the LCD with your decide text. Make the text move from left to right and display
the temperature and light values of the LM35 temperature sensor and the LDR.

WWW.BITSTOC.COM BitStoc Electronics Page 31


Project #9: Servo Motors _

Fig. 9.1 Fig. 9.2

Materials Needed:
1x Arduino UNO board
1x Breadboard (half-size)
1x Servo Motor

Construct the circuit with correct connections and part orientation by following the graphical
representation shown in Fig. 9.1. Your actual circuit will look like in Fig. 9.2.

Copy the Arduino code from Sample Code 9.1 and paste it in the Arduino IDE. Compile and Upload
the code to your Arduino board by following the same steps we did in Project number 1.

WWW.BITSTOC.COM BitStoc Electronics Page 32


Sample Code 9.1
/*
Project 9 - Servo Motor
*/

// include the "Servo.h" Arduino library for the servo motor commands
#include <Servo.h>

// we create our own name ( or called "object" in programming) to use for calling servo commands
Servo myservo;

//assign a variable for storing the value of the servo position from 0 to 180 degrees
int pos = 0;

void setup()
{
// use this command and put the Arduino pin number where we connect our servo motor input
// in this case we connect the servo motor on pin 9
// connection: orange wire to pin 9, red wire to 5V+, brown wire to GND
myservo.attach(9);
}

void loop()
{
// use a "for" loop function to count from 0 to 180 degrees
// the servo turns with a 1 degree per step
for (pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}

// use another "for" loop to reverse the spin of the servo from 180 to 0 degrees
for (pos = 180; pos >= 0; pos -= 1)
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}

The Output

Run the program and you should see that the servo motor turns from 0 to 180 degrees and then turns
back to 180 to 0 degrees.

WWW.BITSTOC.COM BitStoc Electronics Page 33


Project #10: DC Motors ________

Fig. 10.1 Fig. 10.2

Materials Needed:
1x Arduino UNO board 1x Transistor NPN (BC547)
1x Breadboard (half-size) 1x 330 ohm resistor (orange-orange-brown)
1x DC Motor Connecting wires
1x Diode

Construct the circuit with correct connections and part orientation by following the graphical
representation shown in Fig. 10.1. Your actual circuit will look like in Fig. 10.2.

Copy the Arduino code from Sample Code 10.1 and paste it in the Arduino IDE. Compile and Upload
the code to your Arduino board by following the same steps we did in Project number 1.

WWW.BITSTOC.COM BitStoc Electronics Page 34


Sample Code 10.1
/*
Project 10 - DC Motor
*/

// assign variable for the arduino pin connected to DC motor


int motorPin = 9;

// the setting up of pins.


void setup()
{
// Call a Serial Monitor to use for entering a speed for the DC motor
// and assign the DC motor arduino pin as output.
Serial.begin(9600);
pinMode(motorPin, OUTPUT);
Serial.println("Type a speed from 0 to 255 in the box above and hit Enter");
}

void loop()
{
// We use "if" statement to check if a value (speed from 0 to 255) is entered in the serial monitor.
// If there is, we use parseInt() function to get all the numbers.
// Then, use analogWrite() function to use that value to drive the DC motor speed.
if (Serial.available())
{
int motorSpeed = Serial.parseInt();
if (motorSpeed >= 0 && motorSpeed <= 255)
{
analogWrite(motorPin, motorSpeed);
}
}
}

The Output

Run the program and open the Serial Monitor. Type a value to set a speed for the dc motor.
Theoretically we can use from 0, but some motors we use have higher specification, so typically we can
start to use a value from 150 to 255 (maximum). Type in value from 150 to 255 then hit Enter or Send
button. You will see that the DC motor turns faster when the value is at 255 and get slower turn if set to
255 below.

WWW.BITSTOC.COM BitStoc Electronics Page 35


Other things to do

Copy and upload the code below. This program will make the DC motor start and stop from turning for
every 2 seconds. Try to play with the Run and Stop values to make the motor spin longer or shorter.

Sample Code 10.2


/*
Project 10 - DC Motor, code 2
*/

// assign variable for the arduino pin connected to DC motor


int motorPin = 9;

void setup()
{
pinMode(motorPin, OUTPUT); // set the motorPin to output
}

void loop()
{
// Call the function "motorRunandStop()" to run repeatedly in
motorRunandStop();
}

// We create an outside function to make the DC motor Run and Stop for 2 seconds
void motorRunandStop()
{
int Run = 2000; // milliseconds to turn the motor on
int Stop = 2000; // milliseconds to turn the motor off

digitalWrite(motorPin, HIGH); // turn ON the DC motor


delay(Run); // delay for 2000 mS (2 seconds)
digitalWrite(motorPin, LOW); // turn OFF the DC motor
delay(Stop); // delay for 2000 mS (2 seconds)
}

WWW.BITSTOC.COM BitStoc Electronics Page 36


Other Examples, Tutorials and Guides___________________________

Going further Check out the other Example Codes, Projects, Circuits and Guides from the following links.

https://www.arduino.cc/en/Tutorial/HomePage

https://www.arduino.cc/en/Reference/HomePage

https://www.arduino.cc/en/Guide/HomePage

WWW.BITSTOC.COM BitStoc Electronics Page 37


WWW.BITSTOC.COM BitStoc Electronics Page 38

Das könnte Ihnen auch gefallen