Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

67
Monster Kit Projects Page 1 BotShop.co.za

Transcript of Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Page 1: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Monster Kit Projects

Page 1 BotShop.co.za

Page 2: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 1. Blinking LED with resistors

Project description

This tirst project will simply switch a LED on and ott indetinitely or until you remove power trom

your Arduino board. The project consists ot one LED with a resistor connected between the LED

and the power. This resistor is used to bring the volts to the LED down and to limit the current so

that the LED don't get damaged.

Project Schematic diagram

In tuture projects, we will not show the tull schematic with the tull Arduino micro-controller as

shown in the above diagram. We will use a diagram like below where the number 13 will mean pin

13 on your Arduino board, the ground will mean any ot the ground connections that are available on

the Arduino board.

Page 2 BotShop.co.za

Page 3: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Parts required

1 x Arduino Uno, 1x breadboard, 1x led, 1 x 220 Ohm resistor and 2 x jumper cables

Breadboard diagram

Sketch

The sketch you will have to upload is the usual blink sketch in the Arduino IDE examples section. It

is the same one we went through in the “getting started” manual in detail. The only ditterence is that

we do not use the on-board LED but we tit an external LED.

Exercises

1. Put the jumper that is connected to pin 13 on pin 8 and change the sketch code so the led

will now work trom pin 8.

2. Change the delay times so that the LED tlashes taster or slower.

Page 3 BotShop.co.za

Page 4: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 2. 8 Led sequence

Project details

We have caused one LED to blink, now it's time to up the stakes. Let's connect eight ot them. We'll

also have an opportunity to stretch the Arduino a bit by creating various lighting sequences. This

circuit is also a nice setup to experiment with writing your own programs and getting a teel tor how

the Arduino works. We will also expand your programming skills in this project.

Project schematic

Parts required

1 x Arduino Uno, 1x breadboard, 8x LEDs, 8 x 220 Ohm resistors and 9 x Jumper cables

Page 4 BotShop.co.za

Page 5: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Breadboard layout

Sketch 1

int ledPin2=2;

int ledPin3=3;

int ledPin4=4;

int ledPin5=5;

int ledPin6=6;

int ledPin7=7;

int ledPin8=8;

int ledPin9=9;

void setup() {

//define all pins as output pins

pinMode(ledPin2,OUTPUT);

pinMode(ledPin3,OUTPUT);

pinMode(ledPin4,OUTPUT);

pinMode(ledPin5,OUTPUT);

Page 5 BotShop.co.za

Page 6: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

pinMode(ledPin6,OUTPUT);

pinMode(ledPin7,OUTPUT);

pinMode(ledPin8,OUTPUT);

pinMode(ledPin9,OUTPUT);

}

void loop() {

//switch led's on one at a time

digitalWrite(ledPin2, HIGH);

delay(1000);

digitalWrite(ledPin3, HIGH);

delay(1000);

digitalWrite(ledPin4, HIGH);

delay(1000);

digitalWrite(ledPin5, HIGH);

delay(1000);

digitalWrite(ledPin6, HIGH);

delay(1000);

digitalWrite(ledPin7, HIGH);

delay(1000);

digitalWrite(ledPin8, HIGH);

delay(1000);

digitalWrite(ledPin9, HIGH);

delay(2000);

//switch led's off one at a time

digitalWrite(ledPin2, LOW);

delay(1000);

digitalWrite(ledPin3, LOW);

delay(1000);

digitalWrite(ledPin4, LOW);

delay(1000);

digitalWrite(ledPin5, LOW);

delay(1000);

digitalWrite(ledPin6, LOW);

delay(1000);

digitalWrite(ledPin7, LOW);

delay(1000);

digitalWrite(ledPin8, LOW);

delay(1000);

Page 6 BotShop.co.za

Page 7: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

digitalWrite(ledPin9, LOW);

delay(2000);

}

Wow, that is a lot ot code to get something as basic as running LEDs to work. Imagine having many

ditterent LED sequences?

Sketch 2

We can make the code shorter by creating arrays and using a tor loop.

int ledPin[] = {2,3,4,5,6,7,8,9}; //we create an array for all those pins

void setup() {

//define all pins as output pins

for(int i = 0; i < 8; i++){

pinMode(ledPin[i],OUTPUT);

}

}

void loop() {

//switch led's on one at a time

for(int i = 0; i < 8; i++){

digitalWrite(ledPin[i], HIGH);

delay(1000);

}

delay (1000);

//switch led's off one at a time

for(int i = 0; i < 8; i++){

digitalWrite(ledPin[i], LOW);

delay(1000);

}

}

Page 7 BotShop.co.za

Page 8: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 3. Motors

Project details

Not just Arduino, but all microprocessors can only supply tiny amounts ot current directly trom its

pins. In tact, your Arduino pins can supply a maximum of 20mA!

In many cases, this current is more than enough tor many components like temperature sensors,

led's and so on. When you are going to use power hungry components and equipment like motors

we will need to supply those components with its own power and then switch that power using

transistors or relays with our Arduino.

The chances that the motor with spin without the transistors are good but you will run the risk ot

permanently damaging your Arduino when the motor is connected to something that makes it work

hard to turn.

The whole idea behind this project is to teach you how to use a low current device to control high

current devices.

Project diagram

Parts required

1 x Arduino Uno, 1x breadboard, 1x 5V motor, 1 x 1K Ohm resistor, 1 x NPN transistor

(BC547) 5 x Jumper cables, 1 x diode

Page 8 BotShop.co.za

Page 9: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Sketch

int motorPin = 9; // define the pin the motor is connected to

void setup()

Page 9 BotShop.co.za

Page 10: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

{

pinMode(motorPin, OUTPUT);

}

void loop() // run over and over again

{

motorOnThenOff();

}

void motorOnThenOff(){

int onTime = 2500; //the number of milliseconds for the motor to turn on for

int offTime = 1000; //the number of milliseconds for the motor to turn off for

digitalWrite(motorPin, HIGH); // turns the motor On

delay(onTime); // waits for onTime milliseconds

digitalWrite(motorPin, LOW); // turns the motor Off

delay(offTime); // waits for offTime milliseconds

}

Page 10 BotShop.co.za

Page 11: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 4. Servo'sServo's are usetul because you can instruct these small motors how tar to turn, and they do it tor

you. In robotics, it's used tor many things like steering a robot or sweeping an area lett to right with

an ultrasonic sensor connected tor tinding obstacles.

One ot the great teatures ot the Arduino is it has a sottware library that allows you to control two

servos (connected to pin 9 or 10) using a single line ot code. They’re usetul because you can

instruct these small motors how much to turn, and they do it tor you.

In this project we will make a servo sweep lett and right continuesly.

Parts required

1 x Arduino Uno, 1x breadboard, 1x hobby servo, 5 x Jumper cables

Page 11 BotShop.co.za

Page 12: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Servo connectionA servo has 3 connection wires. One tor power, one tor ground and a signal wire. The signal wire

plugs into one ot your Arduino digital PWM pins to control the servo. This makes connecting a

servo extremely easy. Thanks to the electronics inside the servo no additional components are

required to the servo.

In the code below we use digital pin 9 to connect to the servo data wire.

Code

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position

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

}

}

Page 12 BotShop.co.za

Page 13: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 5. PotentiometersAnother name tor a potentiometer is a variable resistor. You can turn a “pot” clockwise or anti-clock

wise to get ditterent resistance values.

Project details: This project will control the speed a led tlashes on and ott.

Connecting a pot to an Arduino.

Parts required

1 x Arduino Uno, 1x breadboard, 1x 10k pot, 1 x 220 Ohm resistor, 5 x Jumper cables

Page 13 BotShop.co.za

Page 14: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

The sketch

int sensorPin = 0; // select the analogue input pin for the potentiometer

int ledPin = 13; // select the pin for the LED

int sensorValue = 0; // variable to store the value coming from the sensor

void setup() {

pinMode(ledPin, OUTPUT); //declare the ledPin as an OUTPUT:

}

void loop() {

sensorValue = analogRead(sensorPin);// read the value from the sensor:

digitalWrite(ledPin, HIGH); // turn the ledPin on

delay(sensorValue); // stop the program for <sensorValue> milliseconds:

digitalWrite(ledPin, LOW); // turn the ledPin off:

delay(sensorValue); // stop the program for <sensorValue> milliseconds:

}

Page 14 BotShop.co.za

Page 15: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 6. Servo control

It's time to start combining some projects to make them more useful.

In this project we will combine 2 previous projects. We will use a potentiometer to control a servo.

This project is an improvement on our previous servo project where the servo just sweeps trom lett

to right and back again. This time we will be using a potentiometer so that we can control the servo

ourselves.

Parts required

1 x Arduino Uno, 1x breadboard, 1x 10k pot, 1 x servo, 8 x Jumper cables

Page 15 BotShop.co.za

Page 16: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Code

#include <Servo.h> // insert the Servo.h library

Servo myservo; // create servo object to control servo

int potpin = 0; // connect potentiometer to digital pin0

int val; // variable value to read value from analog pin

void setup() {

myservo.attach(9); //Attach the servo on pin 9 to the servo object.

}

void loop() {

val = analogRead(potpin); //eads the value of the potentiommeter (value between 0 and 1023)

val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)

Page 16 BotShop.co.za

Page 17: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

myservo.write(val); // sets the servo position according to the scaled value

delay(15);

}

Project 7. 8 LEDS using only 3 digital pinsThis is not just limited to LEDs, you can also control any other digital devices like relays the same

way.

To do that we need an integrated circuit called a shitt register. A shitt register allow us to use only 3

digital pins to control many LEDs, you can also connect many shitt registers together to control

even more LEDs and still just use 3 pins.

Page 17 BotShop.co.za

Page 18: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Parts required

1 x Arduino Uno, 1x breadboard, 1x Shitt Register 74HC595, 8 x leds, 8 x 220Ohm resistors,

15 x Jumper cables

Page 18 BotShop.co.za

Page 19: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Code

int latchPin = 5;

int clockPin = 6;

int dataPin = 4;

byte leds = 0;

void setup()

{

pinMode(latchPin, OUTPUT);

pinMode(dataPin, OUTPUT);

pinMode(clockPin, OUTPUT);

}

void loop()

{

leds = 0;

updateShiftRegister();

delay(500);

for (int i = 0; i < 8; i++)

{

bitSet(leds, i);

updateShiftRegister();

delay(500);

}

}

void updateShiftRegister()

{

digitalWrite(latchPin, LOW);

shiftOut(dataPin, clockPin, LSBFIRST, leds);

digitalWrite(latchPin, HIGH);

}

Page 19 BotShop.co.za

Page 20: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 8. Buzzers

Project description: Sound is an analogue phenomena, how will our digital Arduino cope? We will

once again rely on its incredible speed which will let it mimic analogue behaviour. To do this, we

will attach a buzzer to one ot the Arduino's PWM pins. A buzzer makes a clicking sound each time

it is pulsed with current. It we pulse it at the right trequency (tor example 440 times a second to

make the note middle A) these clicks will run together to produce notes.

Let's get to experimenting with it and get your Arduino playing "Twinkle Twinkle Little Star".

Parts required

1 x Arduino Uno, 1x breadboard, 1x passive, 4 x Jumper cables

Page 20 BotShop.co.za

Page 21: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

int speakerPin = 9;

int length = 15; // the number of notes

char notes[] = "ccggaagffeeddc "; // a space represents a rest

int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };

int tempo = 300;

void playTone(int tone, int duration) {

for (long i = 0; i < duration * 1000L; i += tone * 2) {

digitalWrite(speakerPin, HIGH);

delayMicroseconds(tone);

digitalWrite(speakerPin, LOW);

delayMicroseconds(tone);

}

}

void playNote(char note, int duration) {

Page 21 BotShop.co.za

Page 22: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };

int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

// play the tone corresponding to the note name

for (int i = 0; i < 8; i++) {

if (names[i] == note) {

playTone(tones[i], duration);

}

}

}

void setup() {

pinMode(speakerPin, OUTPUT);

}

void loop() {

for (int i = 0; i < length; i++) {

if (notes[i] == ' ') {

delay(beats[i] * tempo); // rest

} else {

playNote(notes[i], beats[i] * tempo);

}

// pause between notes

delay(tempo / 2);

}

}

Page 22 BotShop.co.za

Page 23: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 9. Relays

Warning: Relays are otten used with high power equipment like the mains trom your house.

This power can kill you! Please do not attach high power equipment to your relay project, we will

not take any responsibility tor any injuries – your relay can handle it you can't.

From the warning above it is clear that relays can be used to switch high power equipment like

220V lights, motors and so on. It can also be used to switch low power equipment like 12V motors

and so on.

Arduino, transistors and relays

Why not just use a transistor?

• Transistors can not switch AC power

• Transistors can not switch very high power

We will still use a transistor when working with Arduino and relays because most relays need more

current to switch on than provided by the Arduino digital pins. You also will tind that some relays

switches at the max voltage an Arduino can supply but that will leave you with instability because

sometimes it will switch and sometimes not. A transistor is always a good idea to use.

Page 23 BotShop.co.za

Page 24: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Parts required

1 x Arduino Uno, 1x breadboard, 1x relay, 1 x NPN transistor, 3 x 220Ohm resistors, 5 x

Jumper cables, 1 x diode (optional over the coil)

Code

*/

int relayPin = 2; // Relay connected to digital pin 2

void setup() {

// initialize the digital pin as an output:

pinMode(relayPin, OUTPUT);

}

void loop()

{

digitalWrite(relayPin, HIGH); // set the LED on

delay(1000); // wait for a second

digitalWrite(relayPin, LOW); // set the LED off

delay(1000); // wait for a second

}

Page 24 BotShop.co.za

Page 25: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 10 Push buttons

A push button is a straight torward component but not so straight torward to connect to an Arduino

because we need something called a pull-up resistor.

Project details

Our project is a simple one, we will tirst just use one button that will turn a led on while the button

is pressed. Then we will use two buttons that we will use to switch the led on with the tirst button

and use the second button to switch the led ott.

How to use push buttons with your Arduino.

With Arduino we most otten use push buttons to send volts to an Arduino digital pin, the Arduino

will pick up that it just received volts on the pin and you can write code to tell Arduino what to do

when it receives volts on a digital pin. When a push button is not pushed to make a connection it

means the push button tloats in the air and can pick up all kinds ot static electricity and so on,

making it behave unpredictable and weird. We use a pull-up or pull-down resistor to stop this trom

happening.

Page 25 BotShop.co.za

Page 26: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Parts required

1 x Arduino Uno, 1x breadboard, 2x push buttons, 1 x leds, 2 x 10k resistors, 1 x 220 Ohm

resistor, 10 x Jumper cables

Page 26 BotShop.co.za

Page 27: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

1 button code

int buttonPin = 2; // the number of the pushbutton pin

int ledPin = 13; // the number of the LED pin

int buttonState = 0; // variable for reading the pushbutton status

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);

Page 27 BotShop.co.za

Page 28: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

// check if the pushbutton is pressed.

// if it is, the buttonState is HIGH:

if (buttonState == HIGH) {

// turn LED on:

digitalWrite(ledPin, HIGH);

}

else {

// turn LED off:

digitalWrite(ledPin, LOW);

}

}

2 button code

int ledPin = 13; // choose the pin for the LED

int inputPin1 = 3; // button 1

int inputPin2 = 2; // button 2

void setup() {

pinMode(ledPin, OUTPUT); // declare LED as output

pinMode(inputPin1, INPUT); // make button 1 an input

pinMode(inputPin2, INPUT); // make button 2 an input

}

void loop(){

if (digitalRead(inputPin1) == LOW) {

digitalWrite(ledPin, LOW); // turn LED OFF

} else if (digitalRead(inputPin2) == LOW) {

digitalWrite(ledPin, HIGH); // turn LED ON

}

}

Page 28 BotShop.co.za

Page 29: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 11 Photo resistor (LDR)

This is a project that will make a led shine brighter or less bright depending how much light is

received on the LDR.

We have not looked at sensors yet.

The photo resistor is the tirst sensor we will look at, the photo resistor is also called a Light

Dependant Resistor (LDR). In simple terms, when the sensor detects light, its resistance changes.

The stronger light in the surrounding environment, the lower the resistance value the photo resistor

will read. By reading the photo resistor’s resistance value, we can work out the ambient lighting in

an environment.

Page 29 BotShop.co.za

Page 30: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Parts required

1 x Arduino Uno, 1x breadboard, LDR, 1 x LED, 1 x 10k resistors, 1 x 220 Ohm resistor, 6 x

Jumper cables

Code to see values from the sensor

int val = 0;

void setup() {

Serial.begin(9600);

}

void loop() {

val = analogRead(0); //reads value from analogue pin 0

Serial.println(val);

delay(500);

}

Open up serial monitor to see the values trom the LDR.

Main project code.

Now that we have the two values we can use it in our main project sketch. We use the value 0 tor no

light and 900 tor tull light. Change the sketch below and add your values in this section:

Page 30 BotShop.co.za

Page 31: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

lightLevel = map(lightLevel, 0, 900, 0, 255);

Main code.

//PhotoResistor Pin

int lightPin = 0; //the analog pin the photoresistor is connected to the

//photoresistor is not calibrated to any units so

//this is simply a raw sensor value (relative light)

int ledPin = 9; //the pin the LED is connected to we are controlling brightness so

//we use one of the PWM (pulse width modulation pins)

void setup()

{

pinMode(ledPin, OUTPUT); //sets the led pin to output

}

void loop()

{

int lightLevel = analogRead(lightPin); //Read the lightlevel

lightLevel = map(lightLevel, 0, 900, 0, 255);

//adjust the value 0 to 900 to span 0 to 255

lightLevel = constrain(lightLevel, 0, 255);//make sure the value is between

//0 and 255

analogWrite(ledPin, lightLevel); //write the value

}

Page 31 BotShop.co.za

Page 32: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 12 Photo resistor (LDR) and 8 ledintensity meter

In this project, we will use the LDR with 8 LEDs to measure light intensity. The 8 LEDs will act as

a light intensity meter. The higher the light intensity is, the more the LED is lit. At the end is also an

example ot a day/night switch you can try.

Parts required

1 x Arduino Uno, 1x breadboard, 1 x LDR, 8 x leds, 1 x 10k resistors, 8 x 220 Ohm resistor,

10 x Jumper cables

Page 32 BotShop.co.za

Page 33: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

const int NbrLEDs = 8;

const int ledPins[] = { 5, 6, 7, 8, 9, 10, 11, 12};

const int photocellPin = A0;

const int wait = 30;

// Swap values of the following two constants if cathodes are connected to Gnd

const boolean LED_ON = HIGH;

const boolean LED_OFF = LOW;

int sensorValue = 0; // value read from the sensor

int ledLevel = 0; // sensor value converted into LED 'bars'

void setup() {

Page 33 BotShop.co.za

Page 34: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

for (int led = 0; led < NbrLEDs; led++)

{

pinMode(ledPins[led], OUTPUT); // make all the LED pins outputs

}

pinMode(13, OUTPUT);

}

void loop() {

//sensorValue = analogRead(analogInPin); // read the analog in value

sensorValue = analogRead(photocellPin);

ledLevel = map(sensorValue, 300, 1023, 0, NbrLEDs); // map to the number of LEDs

for (int led = 0; led < NbrLEDs; led++)

{

if (led < ledLevel ) {

digitalWrite(ledPins[led], LED_ON); // turn on pins less than the level

}

else {

digitalWrite(ledPins[led], LED_OFF); // turn off pins higher than

// the level

}

}

if (ledLevel < 3) {

digitalWrite(13, HIGH);

}

if (ledLevel > 3) {

digitalWrite(13, LOW);

}

}

An idea to try yourself – add a relay for a day/night switch.

To make a day/night switch trom the code above is quite easy. You can keep the lightbar code and

circuit above to make your project more impressive.

1. Hook up a relay (see the relay project) on pin 13.

2. Put the relay in OUTPUT mode in the setup() tunction.

pinMode(13, OUTPUT);

3. In the loop() tunction you want to create 2 new commands, the tirst is when your relay should

switch on and another value when it should be switched ott. A day night switch does not dim or

Page 34 BotShop.co.za

Page 35: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

bright, a light goes on when it is getting dark and ott when the sun comes out to save on electricity

when you torget to switch ott the lights in the daytime.

if (ledLevel < 3) {

digitalWrite(13, HIGH);

}

if (ledLevel > 3) {

digitalWrite(13, LOW);

}

You can change the on/ott values above to get better results. It you look at the main sketch the

ledLevel variable reters to the number ot LEDs that should be on as accomplished trom using the

map tunction.

We can thus look at how many LED's are on when we want the relay to switch on. In our code

above we say that it there are only 2 or tewer LEDs on it should switch the relay on and it there are

4 or more LEDs on to switch the relay on.

You will note that nothing will happen it there are 3 LEDs on (we haven't written any code tor that

condition), that is to compensate tor that time ot day when it's just nearing sun up or sun down so

your relay does not shutter uncontrollably on and ott during that time.

Page 35 BotShop.co.za

Page 36: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 13 Tilt switchThe tilt-switch we use is a ball tilt-switch with a metal ball inside. It looks very similar to a capacitor.

The principle ot the tilt switch is very simple. It mainly uses the ball in the switch to detect tilting ot

something like a robot chassis and trigger an Arduino digital pin The tilt switch will conduct, or it will breakdepending on where the ball is. Simple, but ettective.

Page 36 BotShop.co.za

Page 37: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Parts required

1 x Arduino Uno, 1x breadboard, 1 x Tilt switch, 4 x Jumper cables

/*****************************************/

const int ledPin = 13;//the led attach to the Arduino Uno board

void setup()

{

pinMode(ledPin,OUTPUT);//initialize the ledPin as an output

pinMode(2,INPUT);

digitalWrite(2, HIGH);

}

/******************************************/

void loop()

{

Page 37 BotShop.co.za

Page 38: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

int digitalVal = digitalRead(2);

if(HIGH == digitalVal)

{

digitalWrite(ledPin,LOW);//turn the led off

}

else

{

digitalWrite(ledPin,HIGH);//turn the led on

}

}

/**********************************************/

Note that we use the on-board LED to see what happens when you tilt your breadboard, you can

hookup a LED to your Arduino it you wish.

Page 38 BotShop.co.za

Page 39: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 14 Temperature sensor

Temperature sensors came in many ditterent package sizes as well as with ditterent temperature

measuring capabilities.

We will be using the LM35DZ temperature sensor, its a complicated IC (integrated circuit) that

looks like a transistor.

It has three pins:

• ground

• signal

• VCC

Parts required

1 x Arduino Uno, 1x breadboard, 1 x lm35 temp sensor, 5 x Jumper cables

Page 39 BotShop.co.za

Page 40: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

int temperaturePin = 0;

void setup()

{

Serial.begin(9600);

}

void loop() // run over and over again

{

float temperature = getVoltage(temperaturePin); //getting the voltage reading from the temperature sensor

temperature = (temperature - .5) * 100; //converting from 10 mv per degree with 500

// mV offset to degrees ((voltagge - 500mV) times 100)

Serial.println(temperature); //printing the result

delay(1000); //waiting a second

}

/*

* getVoltage() - returns the voltage on the analog input defined by

* pin

Page 40 BotShop.co.za

Page 41: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

*/

float getVoltage(int pin){

return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range

// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts

}

Page 41 BotShop.co.za

Page 42: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 15 RGB LEDs

In this project, we will create ditterent colours using an RGB led.

RGB LEDs

The RGB LED has tour leads. It you are using a common cathode RGB LED, there is one lead

going to the negative connection ot each ot the single LEDs and a single lead that is connected to

all three positive sides ot the LEDs.

By assigning ditterent values ot brightness to the 3 primary colours using PWM we can create any

value we like. From previous discussions, we said that a PWM pin can have any value between 0

and 255 where 0 is 0V and 255 is 5V.

Page 42 BotShop.co.za

Page 43: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Parts required

1 x Arduino Uno, 1x breadboard, 1 x RGB LED, 3 x 220 Ohm resistors, 4 x Jumper cables

Page 43 BotShop.co.za

Page 44: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Code

int redPin = 9;

int greenPin = 10;

int bluePin = 11;

void setup(){

pinMode(redPin, OUTPUT);

pinMode(greenPin, OUTPUT);

pinMode(bluePin, OUTPUT);

}

void loop(){

//R:0-255 G:0-255 B:0-255

colorRGB(random(0,255),random(0,255),random(0,255));

delay(1000);

}

void colorRGB(int red, int green, int blue){

analogWrite(redPin,constrain(red,0,255));

analogWrite(greenPin,constrain(green,0,255));

analogWrite(bluePin,constrain(blue,0,255));

}

Page 44 BotShop.co.za

Page 45: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 16 RGB led, pot and push button

In this project, we will use a pot and push button to change each colour to whatever we want. The

push button will be used to select the colour to be changed and the pot to change the brightness ot

the selected colour.

Parts required

1 x Arduino Uno, 1x breadboard, 1 x RGB LED, 3 x 220 Ohm resistors, 1 x push button, 1 x

10k pot, 11 x Jumper cables

Page 45 BotShop.co.za

Page 46: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

int redPin = 11;// R – digital 9

int greenPin = 10;// G – digital 10

int bluePin = 9;// B – digital 11

int potPin = 0;// potentiometer 1 – analogue 0

int buttonPin = 1;

int buttonStatus = 3;

int potValue;

void setup(){

pinMode(redPin,OUTPUT);

pinMode(greenPin,OUTPUT);

pinMode(bluePin,OUTPUT);

pinMode(buttonPin, INPUT_PULLUP);

}

void loop(){

int state = digitalRead(buttonPin);

if (state == LOW){

Page 46 BotShop.co.za

Page 47: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

if (buttonStatus < 3){

buttonStatus=buttonStatus + 1;

}

else{

buttonStatus = 1;

}

}

switch (buttonStatus){

case 1:

potValue = analogRead(potPin);

potValue = map(potValue,0,1023,0,255);

analogWrite(redPin,constrain(potValue,0,255));

break;

case 2:

potValue = analogRead(potPin);

potValue = map(potValue,0,1023,0,255);

analogWrite(greenPin,constrain(potValue,0,255));

break;

case 3:

potValue = analogRead(potPin);

potValue = map(potValue,0,1023,0,255);

analogWrite(bluePin,constrain(potValue,0,255));

break;

}

}

Page 47 BotShop.co.za

Page 48: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 17 Infra-red remote control

NOTE: This is a module straight from our online training. We added this

module to show you the difference between a basic project and an online

training project that contain much more detail. This module contains double the

number of pages compared to a standard project module.

An intra-red receiver, or IR receiver, is a hardware component that receives IR intormation trom an

intra-red remote controller. A remote controller comes with many buttons and each button will have

a unique code sent with an intra-red signal to an intra-red receiver that identities the button.

This code is then used in order to convert signals trom the remote control into a tormat that can be

understood by the other device.

Because intra-red is light, it requires line-ot-sight visibility tor the best possible operation, the

signals can however still be retlected by items such as glass and walls. Poorly placed IR receivers

can result in what is called "tunnel vision", where the operational range ot a remote control is

reduced because they are set so tar back into the chassis ot a device.

When working with IR receivers and transmitters (remote controllers) there are 2 major stepsto take:

1. Find the unique code ot each button you will use on the IR controller

2. Once you have that codes you use it statements in your sketch to do “something” when the

IR receiver picks up that code.

The tirst sketch is used to tind the code ot a button press.

Parts required

1 x Arduino Uno, 1x breadboard, 1 x LED, 1 x 220 Ohm resistor, 1 x remote controller, 1 x IR

receiver, 5 x Jumper cables

Page 48 BotShop.co.za

Page 49: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

#include <IRremote.h> // insert IRremote.h library

int RECV_PIN = 11; //define the pin of RECV_PIN 11

IRrecv irrecv(RECV_PIN); //define RECV_PIN as infrared receiver

decode_results results; //define variable results to save the result of infrared receiver

void setup(){

Serial.begin(9600); // configure the baud rate 9600

irrecv.enableIRIn(); //Boot infrared decoding

}

void loop() {

//test if receive decoding data and save it to variable results

if (irrecv.decode(&results)) {

// print data received in a hexadecimal

Serial.println(results.value, HEX);

irrecv.resume(); //wait for the next signal

}

}

In this code above we will use a library called “IrRemote.h”.

Page 49 BotShop.co.za

Page 50: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Download this library www.botshop.co.za/libs/AIRremote.zip and add the library to your Arduino

IDE (sketch→include library → add zip library).

Atter you uploaded the sketch open the serial monitor. When you press a button on the remote

controller you will see the button code in the serial monitor. It would be similar to this

10EFD827

FFFFFFFF

10EFD827 would be your code which you will use and the “FFFFFFFF” is when you push too long

you Arduino will receive this code, always use the tirst one trom last button press.

NOTE: It is very common with IR receivers that you will see 2 or 3 ditterent codes. You will even

see it with a normal home remote control. Now and then you have to press the button more than

once to get a TV or something to respond.

Most people think the code was not received by the remote but the IR receiver actually interpreted

the code wrong, mostly because ot not pointing the remote straight to the receiver and because ot

environment interterence. But also because ot the IR technology itselt is not a high precision

technology, its main advantage is that it is cheap and easy to use by the consumer market.

The only thing you can really do is to have the remote transmitter's "eye" as close as possible to the

receiver's "eye" and press the button a couple ot times and take the code that comes up most ot the

times.

In code output, it looks very inconsistent (because it is), but practically it is not that bad.

Write down the codes of at least three buttons so we can use it in an it statement to turn a led on

or ott.

The code below will start the IR library and must be placed in the setup() tunction.

irrecv.enableIRIn()

The code below is used to constantly read values trom the IR receiver.

if (irrecv.decode(&results)) {

unsigned int value = results.value;

Serial.println(value);

irrecv.resume(); // Receive the next value

}

}

Basically what happens above is that it there is a value received trom the IR receiver the

irrecv.decode call to the library will convert the code it received into a hexadecimal.

Page 50 BotShop.co.za

Page 51: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

The original intra-red decoding is very complicated to manipulate, that is why we use the library

that will do this tor us.

Serial.println(value);

The above code will print the result trom the library

The final code to switch a LED with the remote controller.

Now that we have the button code it is simple:

#include <IRremote.h>

int RECV_PIN = 11;

int ledPin = 13; // LED – digital 10

boolean ledState = LOW; // ledState to store the state of LED

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup(){

Serial.begin(9600);

irrecv.enableIRIn();

pinMode(ledPin,OUTPUT); // define LED as output signal

}

void loop() {

if (irrecv.decode(&results)) {

Serial.println(results.value, HEX);

//once receive code from power button, the state of LED is changed from HIGH

//to LOW or LOW to HIGH

if(results.value == 0x10EFD827){

ledState = !ledState; //reverse

digitalWrite(ledPin,ledState); //change the state of LED

}

irrecv.resume();

}

}

Replace your button HEX value with the button code (10EFD827) in the it statement:

if(results.value == 0x10EFD827)

Important: The code we copied trom our previous sketch is in hexadecimal but Arduino by detault

will think it works with normal numbers and letters. By adding 0x in tront ot your code, Arduino

will now know that it works with a hexadecimal number.

We will be using the Arduino on-board LED just to makes thinks a bit simpler, you can also use a

Page 51 BotShop.co.za

Page 52: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

led trom a breadboard as you did many times betore in this training.

There is one simple piece ot code I want to comment on.

ledState = !ledState; //reverse

This code is an extremely easy way to change the state the led is in (either on or ott) every time the

button is pressed.

This code work because the ledState variable is a boolean data type. A boolean data type can only

take one ot two values, either true or talse.

When working with LEDs, true will usually mean the LED is on and talse will mean it is ott.

The piece ot code above uses the “!” operator that means not.

This code below means that the value ot ledState (either true or talse) will be changed or reversed

trom either true to talse or talse to true.

ledState = !ledState; //reverse

Page 52 BotShop.co.za

Page 53: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 18. 8-segment led display

This project uses an 8 segment LED to build a counter.

Parts required

1 x Arduino Uno, 1x breadboard, 1 x LED, 8 x 220 Ohm resistors, 8 segment led display, 1 x

remote controller, 1 x IR receiver, 12 x Jumper cables

Page 53 BotShop.co.za

Page 54: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

void setup(){

for(int pin = 2 ; pin <= 9 ; pin++){ // define digital pin 2-9 as output

pinMode(pin, OUTPUT);

digitalWrite(pin, HIGH);

}

}

void loop() {

// display number 0

int n0[8]={0,0,0,1,0,0,0,1};

//display the array of n0[8] in digital pin 2-9

for(int pin = 2; pin <= 9 ; pin++){

Page 54 BotShop.co.za

Page 55: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

digitalWrite(pin,n0[pin-2]);

}

delay(500);

// display number1

int n1[8]={0,1,1,1,1,1,0,1};

// display the array of n1[8] in digital pin 2-9

for(int pin = 2; pin <= 9 ; pin++){

digitalWrite(pin,n1[pin-2]);

}

delay(500);

// display number 2

int n2[8]={0,0,1,0,0,0,1,1};

// display the array of n2[8] in digital pin 2-9

for(int pin = 2; pin <= 9 ; pin++){

digitalWrite(pin,n2[pin-2]);

}

delay(500);

// display number 3

int n3[8]={0,0,1,0,1,0,0,1};

// display the array of n3[8] in digital pin 2-9

for(int pin = 2; pin <= 9 ; pin++){

digitalWrite(pin,n3[pin-2]);

}

delay(500);

// display number 4

int n4[8]={0,1,0,0,1,1,0,1};

// display the array of n4[8] in digital pin 2-9

for(int pin = 2; pin <= 9 ; pin++){

digitalWrite(pin,n4[pin-2]);

}

delay(500);

// display number 5

int n5[8]={1,0,0,0,1,0,0,1};

// display the array of n5[8] in digital pin 2-9

for(int pin = 2; pin <= 9 ; pin++){

digitalWrite(pin,n5[pin-2]);

}

delay(500);

// display number 6

Page 55 BotShop.co.za

Page 56: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

int n6[8]={1,0,0,0,0,0,0,1};

//display the array of n6[8] in digital pin 2-9

for(int pin = 2; pin <= 9 ; pin++){

digitalWrite(pin,n6[pin-2]);

}

delay(500);

// display number 7

int n7[8]={0,0,1,1,1,1,0,1};

// display the array of n7[8] in digital pin 2-9

for(int pin = 2; pin <= 9 ; pin++){

digitalWrite(pin,n7[pin-2]);

}

delay(500);

// display number 8

int n8[8]={0,0,0,0,0,0,0,1};

// display the array of n8[8] in digital pin 2-9

for(int pin = 2; pin <= 9 ; pin++){

digitalWrite(pin,n8[pin-2]);

}

delay(500);

// display number 9

int n9[8]={0,0,0,0,1,1,0,1};

// display the array of n9[8] in digital pin 2-9

for(int pin = 2; pin <= 9 ; pin++){

digitalWrite(pin,n9[pin-2]);

}

delay(500);

}

Page 56 BotShop.co.za

Page 57: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 19: LCD displayNOTE: This is another module straight from our online training. We added this

module to show you the difference between a basic project and an online

training project that contains much more detail. This module contain double the

number of pages compared to a standard project module.

LCD1602 Display

We will be using the LCD1602 display module in this project. The numbers 1602 reters to the

ability to display 16 characters and that it can display two rows.

They are available in many background colours.

This module has many pins that need to be connected to the Arduino, a total ot 6 pins to your

Arduino's digital pins and 2 more tor the power.

Pin connections:

* LCD RS pin to digital pin 12

* LCD Enable pin to digital pin 11

* LCD D4 pin to digital pin 5

* LCD D5 pin to digital pin 4

* LCD D6 pin to digital pin 3

* LCD D7 pin to digital pin 2

* LCD R/W pin to ground

* LCD VDD pin to Arduino 5V

* LCD VSS pin to Arduino ground

* LCD A and K Pins that control the brightness ot the LED backlight with a potentiometer.

It's a lot ot pins, I know.

Page 57 BotShop.co.za

Page 58: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

There is a much easier solution that uses another module called an I2C board that can be titted to

this display that combines all these wires into only 2 data wires and 2 power wires. One can also

buy LCDs with the I2C converter already on the board.

There are no such boards in the kit, we will do it the ditticult way and learn more.

Parts required

1 x Arduino Uno, 1x breadboard, 1 x 16 x 2 LCD display, 1 x 220 Ohm resistor, 1 x 10 K pot,

16 x Jumper cables

Page 58 BotShop.co.za

Page 59: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Page 59 BotShop.co.za

Page 60: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

We will use a library to make our coding easy…. very easy actually

The library we will use is one ot the detault libraries that comes with your Arduino IDE sottware.

You should be able to copy/paste the code below without having to install the LiquidCrystal library

into the IDE tirst.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 13, 5, 4, 3, 2);

void setup() {

lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.

lcd.clear(); // start with a blank screen

lcd.setCursor(0,0); // set cursor to column 0, row 0 (the first row)

lcd.print("BotShop"); // change text to whatever you like. keep it clean!

lcd.setCursor(0,1); // set cursor to column 0, row 1

lcd.print("IS AWESOME");

}

void loop() {

}

The above sketch have the code in the setup() tunction. The one below is in the loop() tunction,

looping between the two lines.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 13, 5, 4, 3, 2);

void setup() {

lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.

}

void loop() {

lcd.clear(); // start with a blank screen

lcd.setCursor(0,0); // set cursor to column 0, row 0 (the first row)

lcd.print(" BotShop"); // change text to whatever you like. keep it clean!

delay(800);

lcd.clear();

lcd.setCursor(0,0); // set cursor to column 0, row 1

lcd.print(" IS AWESOME");

delay(800);

}

Page 60 BotShop.co.za

Page 61: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

In the above code, you can see that the library do all the work tor us. The code is commented well

so you can easily see what each line does.

As a side note, you can also connect the positive wire trom the pot directly to one ot your digital

pins so you can switch the back-light on or ott trom your code.

Don't break up this project yet, we will add to it in the next project.

Page 61 BotShop.co.za

Page 62: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 20: LCD display and Serial monitor

NOTE: This is another module straight from our online training. We added this

module to show you the difference between a basic project and an online

training project that contains much more detail. This module contain double the

number of pages compared to a standard project module.

To type text into your serial monitor that must be sent to the Arduino open the serial monitor, it is

the button that looks like a magnitying glass on the right ot your IDE:

Once the serial monitor opened you will see a tield where you can type text into and a send button

next to it:

I typed “Hello

World” in the space provided in the picture above. As soon as I press the send button the serial

monitor will send the data via the USB cable to your Arduino.

Doing this right now will send the text to your Arduino but your Arduino will do nothing with it as

we tirst need to:

1. Write code to put the text received into a variable

2. Write code to use that text in the variable to do something tor us.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 13, 5, 4, 3, 2);

String comdata = "";

void setup() {

lcd.begin(16,2); // columns, rows. use 16,2 for a 16x2 LCD, etc.

Serial.begin(9600); // start serial port at 9600 bps:

}

void loop() {

Page 62 BotShop.co.za

Page 63: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

//read string from serial monitor

if(Serial.available()>0) // if we get a valid byte, read analog ins:

{

comdata = "";

while (Serial.available() > 0)

{

comdata += char(Serial.read());

delay(2);

}

lcd.clear();

}

lcd.clear(); // start with a blank screen

lcd.setCursor(0,0); // set cursor to column 0, row 0 (the first row)

lcd.print(comdata); // change text to whatever you like. keep it clean!

delay(800);

}

Capture the text from serial monitor

The tirst thing to do is to start serial monitor in the setup() tunction:

Serial.begin(9600); // start serial port at 9600 bps:

Next we will capture the data trom serial monitor and put that in a variable called comdata:

if(Serial.available()>0) // if we get a valid byte, read analog ins:

{

comdata = "";

while (Serial.available() > 0)

{

comdata += char(Serial.read());

delay(2);

}

lcd.clear();

}

We start ott by looking it there is any data available trom the serial monitor with the

Serial.available() tunction, it there is data trom serial monitor the value will be greater than “0” and

we will execute the code in the curly brackets.

Page 63 BotShop.co.za

Page 64: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

The text trom serial monitor will be received one character at a time, we will use the Serial.read()

tunction to read each ot the characters and add them to the variable comdata. The + sign we

encountered betore and all it does is to add another character to the characters already in the

variable.

All that is lett to do is to place the comdata variable into the lcd.print() tunction so it can be

displayed on the LCD display.

lcd.print(comdata); // change text to whatever you like. keep it clean!

Page 64 BotShop.co.za

Page 65: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Project 21. Stepper motors

A normal motor just turns, either clock wise or anti-clock wise and by controlling the volts supplied

they can turn taster or slower. Don't understand me wrong, normal motors are very usetul especially

in robotics it you want to build a robot with wheels.

A stepper motor is quite ditterent, they are DC motors that move in small, precise steps. They have

multiple coils that are organized in groups called "phases". By energizing each phase in sequence,

the motor will rotate, one step at a time. With a computer controlled stepping, you can achieve very

precise positioning and/or speed control.

Wiring the stepper motor.

Parts required

1 x Arduino Uno, 1x breadboard, 1x ULN2003 IC or ULN2003 driver board , 1 x stepper

motor, 5 x Jumper cables

Page 65 BotShop.co.za

Page 66: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Sketch

#include <Stepper.h>

#define Number_Of_Rotations 2

#define STEPS_PER_MOTOR_REVOLUTION 32

#define STEPS_PER_OUTPUT_REVOLUTION 2048 * Number_Of_Rotations

int StopTime = 100;

Stepper myStepper(STEPS_PER_MOTOR_REVOLUTION, 8, 10, 9, 11);

int Steps;

void setup(){

}

void loop(){

Steps = STEPS_PER_OUTPUT_REVOLUTION;

myStepper.setSpeed(1000);

myStepper.step(Steps);

delay(StopTime);

Steps = - STEPS_PER_OUTPUT_REVOLUTION;

myStepper.setSpeed(1000);

myStepper.step(Steps);

delay(StopTime);

}

Page 66 BotShop.co.za

Page 67: Monster Kit Projects - Arduino, robotics & Raspberry Pi ...

Our online training course

Would you like more details? A better explanation ot the electronics and the code?

Our online training course take each project and explain the code and electronics in great detail.

Included are over a 100 quiz questions and a training torum where we answer your question in

detail.

You can tind out more about our online training on our website. https://www.botshop.co.za

Page 67 BotShop.co.za