Está en la página 1de 24

Experiment 1 – Wi-Fi shield Open/Close LEDs

Activity

This lesson will teach you to turn on and off LEDs using webpage.

Experiment Components:

1 x Wi-Fi shield
3 x LED
Jumper wires

Connection:

1. Mount the Wi-Fi shield to the arduino.


2. Connect the LEDs to pins 11, 12, 13.

Coding:

1. Open up the Arduino IDE and type in the following code:


#include <SoftwareSerial.h>

SoftwareSerial ESP11 = SoftwareSerial(2,3);

#define DEBUG true

void setup()
{
Serial.begin(9600);
ESP11.begin(9600);

pinMode(11,OUTPUT);
digitalWrite(11,LOW);

pinMode(12,OUTPUT);
digitalWrite(12,LOW);

pinMode(13,OUTPUT);
digitalWrite(13,LOW);

sendData("AT+RST\r\n",2000,DEBUG); // reset module


sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access
point
sendData("AT+CIFSR\r\n",1000,DEBUG); // get ip address
sendData("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple
connections
sendData("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on
port 80
}
void loop()
{
if(ESP11.available()) // check if the esp is sending a message
{

if(ESP11.find("+IPD,"))
{
delay(1000); // wait for the serial buffer to fill up (read all
the serial data)
// get the connection id so that we can then disconnect
int connectionId = ESP11.read()-48; // subtract 48 because the
read() function returns
// the ASCII decimal
value and 0 (the first decimal number) starts at 48

ESP11.find("pin="); // advance cursor to "pin="

int pinNumber = (ESP11.read()-48)*10; // get first number i.e.


if the pin 13 then the 1st number is 1, then multiply to get 10
pinNumber += (ESP11.read()-48); // get second number, i.e. if
the pin number is 13 then the 2nd number is 3, then add to the first
number

digitalWrite(pinNumber, !digitalRead(pinNumber)); // toggle pin

// make close command


String closeCommand = "AT+CIPCLOSE=";
closeCommand+=connectionId; // append connection id
closeCommand+="\r\n";

sendData(closeCommand,1000,DEBUG); // close connection


}
}
}

/*
* Name: sendData
* Description: Function used to send data to ESP8266.
* Params: command - the data/command to send; timeout - the time to
wait for a response; debug - print to Serial window?(true = yes,
false = no)
* Returns: The response from the esp8266 (if there is a reponse)
*/
String sendData(String command, const int timeout, boolean debug)
{
String response = "";

ESP11.print(command); // send the read character to the esp8266

long int time = millis();

while( (time+timeout) > millis())


{
while(ESP11.available())
{
// The esp has data so display its output to the serial
window
char c = ESP11.read(); // read the next character.
response+=c;
}
}

if(debug)
{
Serial.print(response);
}

return response;
}

2. Press the Verify/Compile button at the top of the IDE to make sure there
are no errors in your code. If this is successful and no error, click the
Upload button to upload the code to your Arduino.
Experiment 2 – Bluetooth shield change name

Activity

This lesson will teach you to change the name of the Bluetooth.

Experiment Components:

1 x Bluetooth shield

Connection:

1. Mount the Bluetooth shield to the arduino.

Coding:

1. Open up the Arduino IDE and type in the following code:

#include <SoftwareSerial.h>
SoftwareSerial BTSerial(10, 11); // RX | TX

void setup()
{
pinMode(9, OUTPUT); // this pin will pull the HC-05 pin 34 (key pin)
HIGH to switch module to AT mode
digitalWrite(9, HIGH);
Serial.begin(9600);
Serial.println("Enter AT commands:");
BTSerial.begin(38400); // HC-05 default speed in AT command more
}

void loop()
{

// Keep reading from HC-05 and send to Arduino Serial Monitor


if (BTSerial.available())
Serial.write(BTSerial.read());

// Keep reading from Arduino Serial Monitor and send to HC-05


if (Serial.available())
BTSerial.write(Serial.read());}
2. Press the Verify/Compile button at the top of the IDE to make sure there
are no errors in your code. If this is successful and no error, click the
Upload button to upload the code to your Arduino.
Experiment 3 – Bluetooth shield Open/Close LEDs

Activity

This lesson will teach you to turn on and off LEDs using Bluetooth shield
and phone application.

Experiment Components:

1 x Bluetooth shield
1 x LED

Connection:

2. Mount the Bluetooth shield to the arduino.


3. Connect the LED to pin 13.

Coding:

3. Open up the Arduino IDE and type in the following code:


/*
* Bluetooh Basic: LED ON OFF - Avishkar
* Coder - Mayoogh Girish
* Website - http://bit.do/Avishkar
* Download the App :
* This program lets you to control a LED on pin 13 of arduino using a
bluetooth module
*/
char data = 0; //Variable for storing received data
void setup()
{
Serial.begin(9600); //Sets the data rate in bits per second
(baud) for serial data transmission
pinMode(13, OUTPUT); //Sets digital pin 13 as output pin
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data and store it
into variable data
Serial.print(data); //Print Value inside data in Serial
monitor
Serial.print("\n"); //New line
if(data == '1') //Checks whether value of data is equal
to 1
digitalWrite(13, HIGH); //If value is 1 then LED turns ON
else if(data == '0') //Checks whether value of data is equal
to 0
digitalWrite(13, LOW); //If value is 0 then LED turns OFF
}}

4. Press the Verify/Compile button at the top of the IDE to make sure there
are no errors in your code. If this is successful and no error, click the
Upload button to upload the code to your Arduino.
Experiment 4 – Bluetooth shield servo motor

Activity

This lesson will teach you to control servo motor using Bluetooth shield
and phone application.

Experiment Components:

1 x Bluetooth shield
1 x Servo motor

Connection:

1. Mount the Bluetooth shield to the arduino.


2. Connect the LED to pin 13.

Coding:

1. Open up the Arduino IDE and type in the following code:

#include<Servo.h>
int servoPin=13;
Servo servo;
int angle = 0; //Variable for storing received data
char data =0;
void setup()
{
Serial.begin(9600); //Sets the data rate in bits per second
(baud) for serial data transmission
servo.attach(servoPin); //Sets digital pin 13 as output pin
}
void loop()
{
if(Serial.available() > 0) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data and store it
into variable data
Serial.print(data); //Print Value inside data in Serial
monitor
Serial.print("\n"); //New line
if(data == '1')
for(angle = 0; angle < 360; angle++)
{
servo.write(angle);
delay(10);
}

else if(data == '0')


for(angle = 0; angle < 0;)
{
servo.write(angle);
}}}

2. Press the Verify/Compile button at the top of the IDE to make sure there
are no errors in your code. If this is successful and no error, click the
Upload button to upload the code to your Arduino.
Experiment 5 – Bluetooth shield servo motor control

Activity

This lesson will teach you to control the servo motor’s angle using
Bluetooth shield and phone application.

Experiment Components:

1 x Bluetooth shield
1 x Servo motor

Connection:

1. Mount the Bluetooth shield to the arduino.


2. Connect the LED to pin 9.

Coding:

1. Open up the Arduino IDE and type in the following code:


#include <Servo.h>

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


// a maximum of eight servo objects can be created
int pos = 0; // variable to store the servo position
int motor = 0;
void setup()
{
Serial.begin(9600); // initialize serial:
myservo.attach(9); // attaches the servo on pin 9 to the servo
object
Serial.print("Arduino control Servo Motor Connected OK");
Serial.print('\n');
}
void loop()
{
// if there's any serial available, read it:
while (Serial.available() > 0) {
// look for the next valid integer in the incoming serial stream:
motor = Serial.parseInt(); // do it again:
pos = Serial.parseInt(); // look for the newline. That's the end of
your sentence:
if (Serial.read() == '\n') {

myservo.write(pos); // tell servo to go to position in


variable 'pos'
delay(15); // waits 15ms for the servo to reach
the position
// print the three numbers in one string as hexadecimal:
Serial.print("Data Response : ");
Serial.print(motor, HEX);
Serial.print(pos, HEX);
}}}
//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 //
}
//val = analogRead(potpin); // reads the value of the
potentiometer (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)
//myservo.write(val); // sets the servo position
according to the scaled value
//delay(15);

2. Press the Verify/Compile button at the top of the IDE to make sure there
are no errors in your code. If this is successful and no error, click the
Upload button to upload the code to your Arduino.
Experiment 6 – Fingerprint Save

Activity

This lesson will teach you how to save a fingerprint.

Experiment Components:

1 x Fingerprint module

Connection:

1. Connect the positive and negative.


2. Connect the RX of fingerprint scanner to TX of arduino and vice versa.
Coding:

1. Open up the Arduino IDE and type in the following code:

#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>

uint8_t id;

uint8_t getFingerprintEnroll();

SoftwareSerial mySerial(2, 3);


Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()
{
while (!Serial);
delay(500);

Serial.begin(9600);
Serial.println("BSCPE Fingerprint sensor enrollment");

finger.begin(57600); // set the data rate for the sensor serial port

if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");}
else {
Serial.println("Did not find fingerprint sensor :(");
while (1);}}

uint8_t readnumber(void) {
uint8_t num = 0;
boolean validnum = false;
while (1) {
while (! Serial.available());
char c = Serial.read();
if (isdigit(c)) {
num *= 10;
num += c - '0';
validnum = true;}
else if (validnum) {
return num;}}}

void loop() // run over and over again


{
Serial.println("Ready to enroll a fingerprint! Please Type in the ID
# you want to save this finger as...");
id = readnumber();
Serial.print("Enrolling ID #");
Serial.println(id);
while (! getFingerprintEnroll() );}
uint8_t getFingerprintEnroll() {
int p = -1;
Serial.print("Waiting for valid finger to enroll as #");
Serial.println(id);
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println(".");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
break;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
break;
default:
Serial.println("Unknown error");
break;}}
// OK success!
p = finger.image2Tz(1);
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
return p;
default:
Serial.println("Unknown error");
return p;}
Serial.println("Remove finger");
delay(2000);
p = 0;
while (p != FINGERPRINT_NOFINGER) {
p = finger.getImage();}
Serial.print("ID "); Serial.println(id);
p = -1;
Serial.println("Place same finger again");
while (p != FINGERPRINT_OK) {
p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.print(".");
break;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
break;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
break;
default:
Serial.println("Unknown error");
break;}}
// OK success!
p = finger.image2Tz(2);
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
return p;
default:
Serial.println("Unknown error");
return p;}

// OK converted!
Serial.print("Creating model for #"); Serial.println(id);
p = finger.createModel();
if (p == FINGERPRINT_OK) {
Serial.println("Prints matched!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_ENROLLMISMATCH) {
Serial.println("Fingerprints did not match");
return p;
} else {
Serial.println("Unknown error");
return p;}
Serial.print("ID "); Serial.println(id);
p = finger.storeModel(id);
if (p == FINGERPRINT_OK) {
Serial.println("Stored!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_BADLOCATION) {
Serial.println("Could not store in that location");
return p;
} else if (p == FINGERPRINT_FLASHERR) {
Serial.println("Error writing to flash");
return p;
} else {
Serial.println("Unknown error");
return p;
}}

2. Press the Verify/Compile button at the top of the IDE to make sure there
are no errors in your code. If this is successful and no error, click the
Upload button to upload the code to your Arduino.
Experiment 7 – Fingerprint Delete

Activity

This lesson will teach you how to delete a fingerprint.

Experiment Components:

1 x Fingerprint module

Connection:

1. Connect the positive and negative.


2. Connect the RX of fingerprint scanner to TX of arduino and vice versa.
Coding:

1. Open up the Arduino IDE and type in the following code:

#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>

uint8_t getFingerprintEnroll(uint8_t id);

SoftwareSerial mySerial(2, 3);


Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()
{
Serial.begin(9600);
Serial.println("Delete Finger");

finger.begin(57600); // set the data rate for the sensor serial port

if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1);
}
}

void loop() // run over and over again


{
while (!Serial); // For Yun/Leo/Micro/Zero/...
delay(500);
Serial.println("Type in the ID # you want delete...");
uint8_t id = 0;
while (true) {
while (! Serial.available());
char c = Serial.read();
if (! isdigit(c)) break;
id *= 10;
id += c - '0';}
Serial.print("deleting ID #");
Serial.println(id);
deleteFingerprint(id);}
uint8_t deleteFingerprint(uint8_t id) {
uint8_t p = -1;
p = finger.deleteModel(id);
if (p == FINGERPRINT_OK) {
Serial.println("Deleted!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_BADLOCATION) {
Serial.println("Could not delete in that location");
return p;
} else if (p == FINGERPRINT_FLASHERR) {
Serial.println("Error writing to flash");
return p;
} else {
Serial.print("Unknown error: 0x"); Serial.println(p, HEX);
return p;}}

2. Press the Verify/Compile button at the top of the IDE to make sure there
are no errors in your code. If this is successful and no error, click the
Upload button to upload the code to your Arduino.
Experiment 8 – Fingerprint Recognize

Activity

This lesson will teach you how to recognize a fingerprint.

Experiment Components:

1 x Fingerprint module

Connection:

1. Connect the positive and negative.


2. Connect the RX of fingerprint scanner to TX of arduino and vice versa.
Coding:

1. Open up the Arduino IDE and type in the following code:

#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>

int getFingerprintIDez();

SoftwareSerial mySerial(2, 3);


Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()
{
while (!Serial);

Serial.begin(9600);
Serial.println("Adafruit finger detect test");

// set the data rate for the sensor serial port


finger.begin(57600);

if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1);
}
Serial.println("Waiting for valid finger...");
}

void loop() // run over and over again


{
getFingerprintIDez();
delay(50); //don't ned to run this at full speed.
}

uint8_t getFingerprintID() {
uint8_t p = finger.getImage();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image taken");
break;
case FINGERPRINT_NOFINGER:
Serial.println("No finger detected");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_IMAGEFAIL:
Serial.println("Imaging error");
return p;
default:
Serial.println("Unknown error");
return p;}
// OK success!
p = finger.image2Tz();
switch (p) {
case FINGERPRINT_OK:
Serial.println("Image converted");
break;
case FINGERPRINT_IMAGEMESS:
Serial.println("Image too messy");
return p;
case FINGERPRINT_PACKETRECIEVEERR:
Serial.println("Communication error");
return p;
case FINGERPRINT_FEATUREFAIL:
Serial.println("Could not find fingerprint features");
return p;
case FINGERPRINT_INVALIDIMAGE:
Serial.println("Could not find fingerprint features");
return p;
default:
Serial.println("Unknown error");
return p;}
// OK converted!
p = finger.fingerFastSearch();
if (p == FINGERPRINT_OK) {
Serial.println("Found a print match!");
} else if (p == FINGERPRINT_PACKETRECIEVEERR) {
Serial.println("Communication error");
return p;
} else if (p == FINGERPRINT_NOTFOUND) {
Serial.println("Did not find a match");
return p;
} else {
Serial.println("Unknown error");
return p;}
// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print("with confidence of ");
Serial.println(finger.confidence); }

// returns -1 if failed, otherwise returns ID #


int getFingerprintIDez() {
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;
p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;
p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;

// found a match!
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of ");
Serial.println(finger.confidence);
return finger.fingerID; }

2. Press the Verify/Compile button at the top of the IDE to make sure
there are no errors in your code. If this is successful and no error, click
the Upload button to upload the code to your Arduino.

También podría gustarte