Arduino For Beginners
Arduino For Beginners
CONTENTS
01 Getting Started
02 Blink an LED
03 Push Button
04 Fade an LED
05 Scrolling LED
06 Photoresistor Night light
07 Traffic Controller
08 LED Dice
09 Binary Counting
10 Binary Calculator
|2
01 Getting Started
Before you get started on your coding we need to install the Arduino IDE onto your
computer. You may do so by going here and clicking the install for your correct OS. You
may install all the code found in this document here
You can open the code for project two by going to File > Examples > 1.Basics > Blink
|3
Select the serial / COM port that your UNO is attached to: Tools > Port > COMxx
You may see multiple ports shown here. If you are not sure what one your device is connected to,
unplug the uno, plug it back in and see what changed.
When you have the code open and the board connected try pressing the upload button.
You should see some LED’s flashing on your UNO followed by a message that says “Done
Uploading”.
|4
02 Blink an LED
Let’s get started with your first Arduino Project! In this simple example we are going to blink an
LED.
REQUIRED PARTS:
Uno R3 Board
Breadboard
Wires
LED
|5
UPLOADING THE SKETCH:
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output. To use another pin
change 13 to the pin you want to use.
pinMode(13, OUTPUT);
}
|6
03 Push Button
REQUIRED PARTS:
Uno R3
Breadboard
Jumper Wires
USB Cable
LED (5 mm)
Push Button
|7
UPLOADING THE SKETCH:
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
|8
04 Fade an LED
REQUIRED PARTS:
Uno R3 Board
Breadboard
Wires
LED
|9
UPLOADING THE SKETCH:
| 10
05 Scrolling LED
REQUIRED PARTS:
Uno R3 Board
Breadboard
Wires
5x LED
| 11
UPLOADING THE SKETCH:
int timer = 100; // The higher the number, the slower the timing.
void setup() {
// use a for loop to initialize each pin as an output:
for (int thisPin = 3; thisPin < 8; thisPin++) {
pinMode(thisPin, OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
for (int thisPin = 3; thisPin < 8; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);
}
| 12
06 Photoresistor Night light
REQUIRED PARTS:
Uno R3 Board
Breadboard
Wires
LED
Photoresistor
| 13
UPLOADING THE SKETCH:
// We’ll also set up some global variables for the light level:
int lightLevel;
int calibratedlightLevel; // used to store the scaled / calibrated
lightLevel
int maxThreshold = 0; // used for setting the “max” light level
int minThreshold = 1023; // used for setting the “min” light level
void setup()
{
pinMode(ledPin, OUTPUT); // Set up the LED pin to be an output.
Serial.begin(9600);
}
void loop()
{
lightLevel = analogRead(sensorPin); // reads the voltage on the sensorPin
Serial.print(lightLevel);
//autoRange(); // autoRanges the min / max values you see in your room.
| 14
void autoRange()
{
if (lightLevel < minThreshold) // minThreshold was initialized to 1023
-- so, if it’s less, reset the threshold level.
minThreshold = lightLevel;
// Once we have the highest and lowest values, we can stick them
// directly into the map() function.
//
// This function must run a few times to get a good range of bright and
dark values in order to work.
| 15
07 Traffic Controller
REQUIRED PARTS:
Uno R3 Board
Breadboard
Wires
3x LED
| 16
UPLOADING THE SKETCH:
void setup() {
// put your setup code here, to run once:
pinMode(red, OUTPUT);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
delay(3000);
| 17
08 LED Dice
REQUIRED PARTS:
Uno R3 Board
Breadboard
Wires
6x LED
Push Button
| 18
UPLOADING THE SKETCH:
void setup() {
// set all LED pins to OUTPUT
for (int i=first; i<=sixth; i++) {
pinMode(i, OUTPUT);
}
// set buttin pin to INPUT
pinMode(button, INPUT);
void buildUpTension() {
// light LEDs from left to right and back to build up tension
// while waiting for the dice to be thrown
// left to right
for (int i=first; i<=sixth; i++) {
if (i!=first) {
digitalWrite(i-1, LOW);
}
digitalWrite(i, HIGH);
| 19
delay(100);
}
// right to left
for (int i=sixth; i>=first; i--) {
if (i!=sixth) {
digitalWrite(i+1, LOW);
}
digitalWrite(i, HIGH);
delay(100);
}
}
int throwDice() {
// get a random number in the range [1,6]
int randNumber = random(1,7);
#ifdef DEBUG
Serial.println(randNumber);
#endif
return randNumber;
}
| 20
void loop() {
// if button is pressed - throw the dice
pressed = digitalRead(button);
if (pressed == HIGH) {
// remove previous number
setAllLEDs(LOW);
buildUpTension();
int thrownNumber = throwDice();
showNumber(thrownNumber);
}
| 21
09 Binary Counting
REQUIRED PARTS:
Uno R3 Board
Breadboard
Wires
6x LED
2x Push Button
| 22
UPLOADING THE SKETCH:
//Credit
https://create.arduino.cc/projecthub/p-o-i-n-t/project-1-binary-counting-52
47b7?ref=tag&ref_id=kids&offset=32
int l[]={0,0,0,0,0,0},T=6,a,p;
const int b1=12,b2=13;
void setup() {
pinMode(2,OUTPUT);
pinMode(3,OUTPUT);
pinMode(4,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(b1,INPUT);
pinMode(b2,INPUT);
}
void loop() { p=0;
p=digitalRead(b1) ;
while(digitalRead(b1)!=0)
{
}
delay(100); a=T-1;
while(a>=0&&p==1)
{
if(l[a]==1)
{
l[a]=0;
a--;
}
else
{
l[a]=1;
p=0;
}
}
p=digitalRead(b2) ;
while(digitalRead(b2)!=0)
| 23
{
}
delay(100); a=T-1;
while(a>=0&&p==1)
{
if(l[a]==0)
{
l[a]=1;
a--;
}
else
{
l[a]=0;
p=0;
}
}
a=T-1;
for(int c=0;c<T;c++)
{
if(l[c]==1)
digitalWrite(T-c+1,HIGH);
else
digitalWrite(T-c+1,LOW);
a--;
}
}
| 24
10 Binary Calculator
REQUIRED PARTS:
Uno R3 Board
Breadboard
Wires
4x LED
6x Push Button
| 25
UPLOADING THE SKETCH:
//Credit:
https://create.arduino.cc/projecthub/22warehamD/3-bit-binary-calculator-usi
ng-arduino-uno-e9d93b?ref=similar&ref_id=21382&offset=0
int B4pin=2;
int B2pin=4;
int B1pin=6;
int out8=10;
int out4=11;
int out2=12;
int out1=13;
void setup() {
Serial.begin(9600); //turn on serial port
pinMode(A4pin,INPUT); //set all input pins to input
pinMode(A2pin,INPUT);
pinMode(A1pin,INPUT);
pinMode(B4pin,INPUT);
pinMode(B2pin,INPUT);
pinMode(B1pin,INPUT);
void loop() {
int A4val=0; //Set all read values as local variables
int A2val=0;
int A1val=0;
int B4val=0;
int B2val=0;
int B1val=0;
| 26
int Aval; //Set A and B values as local variables
int Bval;
B4val = digitalRead(B4pin);
B2val = digitalRead(B2pin);
B1val = digitalRead(B1pin);
if (A4val==1){
A4valcal = 0;
}
if (A2val==0){
A2valcal = 1;
}
if (A2val==1){
A2valcal = 0;
}
if (A1val==0){
A1valcal = 1;
}
if (A1val==1){
A1valcal = 0;
| 27
}
if (B4val==0){
B4valcal = 1;
}
if (B4val==1){
B4valcal = 0;
}
if (B2val==0){
B2valcal = 1;
}
if (B2val==1){
B2valcal = 0;
}
if (B1val==0){
B1valcal = 1;
}
if (B1val==1){
B1valcal = 0;
}
B4val = B4valcal;
B2val = B2valcal;
B1val = B1valcal;
Serial.print(“B = “);
Serial.print(B4val);
Serial.print(B2val);
Serial.println(B1val);
| 28
Aval=(A4val*4)+(A2val*2)+(A1val*1); //Changing binary value to decimal
value
Bval=(B4val*4)+(B2val*2)+(B1val*1);
if (outval>=4) {
digitalWrite(out4,HIGH);
outval=outval-4;
}
if (outval>=2) {
digitalWrite(out2,HIGH);
outval=outval-2;
}
if (outval>=1) {
digitalWrite(out1,HIGH);
outval=outval-1;
}
| 29
Contact Us
Business: [email protected]
Order Inquiry: [email protected]
Other: [email protected]
www.inventr.io