Arduino For Beginners
Arduino For Beginners
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
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”.
#2 – 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
● USB Data Cable
● LED
● 220 Ohm Resistor
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);
}
Required Parts:
● Uno R3
● Breadboard
● Jumper Wires
● USB Cable
● LED (5 mm)
● Push Button
● 10k Ohm Resistor
● 220 Ohm Resistor
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);
Required Parts:
● Uno R3 Board
● Breadboard
● Wires
● USB Data Cable
● LED
● 220 Ohm Resistor
Uploading the sketch:
int led = 9; // the PWM pin the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5 ; // how many points to fade the LED by
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
#5 – Scrolling LED
Required Parts:
● Uno R3 Board
● Breadboard
● Wires
● USB Data Cable
● 5x LED
● 5x 220 Ohm Resistor
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);
}
Required Parts:
● Uno R3 Board
● Breadboard
● Wires
● USB Data Cable
● LED
● 220 Ohm Resistor
● 10k Ohm Resistor
● Photoresistor
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.
nalogWrite(ledPin, calibratedlightLevel);
a // set the led level based
on the input lightLevel.
}
/******************************************************************
* void autoRange()
*
* This function sets a minThreshold and maxThreshold value for the
* light levels in your setting. Move your hand / light source / etc
* so that your light sensor sees a full range of values. This will
* "autoCalibrate" to your range of input values.
/*****************************************************************/
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.
Required Parts:
● Uno R3 Board
● Breadboard
● Wires
● USB Data Cable
● 3x LED
● 3x 220 Ohm Resistor
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);
Required Parts:
● Uno R3 Board
● Breadboard
● Wires
● USB Data Cable
● 6x LED
● 6x 220 Ohm Resistor
● 10k Ohm Resistor
● Push Button
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);
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;
}
void loop() {
// if button is pressed - throw the dice
pressed = digitalRead(button);
buildUpTension();
int thrownNumber = throwDice();
showNumber(thrownNumber);
}
}
#9 – Binary Counting
Required Parts:
● Uno R3 Board
● Breadboard
● Wires
● USB Data Cable
● 6x LED
● 6x 220 Ohm Resistor
● 2x 10k Ohm Resistor
● 2x Push Button
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)
{
}
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--;
}
}
#10 – Binary Calculator
Required Parts:
● Uno R3 Board
● Breadboard
● Wires
● USB Data Cable
● 4x LED
● 4x 220 Ohm Resistor
● 6x 10k Ohm Resistor
● 6x Push Button
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;
B4val = digitalRead(B4pin);
B2val = d igitalRead(B2pin);
B1val = d igitalRead(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;
}
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;
}
A4val = A4valcal; //setting A and B value to inverted value
A2val = A2valcal;
A1val = A1valcal;
B4val = B4valcal;
B2val = B2valcal;
B1val = B1valcal;
Serial.print("B = ");
Serial.print(B4val);
Serial.print(B2val);
Serial.println(B1val);
erial.println("total = ");
S //printing total value in serial port to
check
Serial.println(outval);
Serial.println("");
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;
}