getting started ardu ez
DESCRIPTION
Getting Started Arduino EzTRANSCRIPT
[00-‐2] Getting Started: Ardu-‐ez
Created by University of Tasmania
[00-‐2] Getting Started – Ardu-‐ez
University of Tasmania 2
Getting Started -‐ Ardu-‐ez
Objective • Explore Ardu-‐ez • Understand how to import libraries • How to troubleshoot
Ardu-‐ez Ardu-‐ez has 20 modules on an Arduino main board.
Input modules Switch (Push button)
Output modules Buzzer, RGBLED, DC Motor and Step Motor
Output (Display) modules FND (7 segments) and RGBLCD
Input (Sensors) modules Light, Sound, Temperature, Humidity, 3-‐axis acceleration, Ultrasonic, Gas, PIR and Piezo Sensor
Communication modules RFID, Bluetooth, Wi-‐Fi
[00-‐2] Getting Started – Ardu-‐ez
University of Tasmania 3
Use Libraries A library is a collection of organized code that provides extra functionality. The language that is used by Arduino is an expanded version of the C++, so the developer is free to use the C++ libraries as well. Using these libraries, an Arduino can communicate data with a computer attached via a USB cable. Several libraries exist for communicating with an Arduino.
To use an existing library in a sketch simply go to the Sketch menu, choose "Import Library", and pick from the libraries available. This will insert an #include statement at the top of the sketch for each header (.h) file in the library's folder. These statements make the public functions and constants defined by the library available to your sketch. They also signal the Arduino environment to link that library's code with your sketch when it is compiled or uploaded1.
In this unit, we will learn how to get inputs from the Switch module. It is not easy to understand how the pins are defined and function.
Switch (Push Buttons)
The switch module is a component that connects two points in a circuit when you press it. The example turns on an LED when you press the button.
We connect three wires to the Arduino board. The first goes from one leg of the pushbutton through a pull-‐up resistor (here 2.2 KOhms) to the 5 volt supply. The second goes from the corresponding leg of the pushbutton to ground. The third connects to a digital i/o pin (here pin 7) which reads the button's state 2.
When the pushbutton is open (un-‐pressed) there is no connection between the two legs of the pushbutton, so the pin is connected to 5 volts (through the pull-‐up resistor) and we read a HIGH. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to ground, so that we read a LOW. (The pin is still connected to 5 volts, but the resistor in-‐between them means that the pin is "closer" to ground.) [Button Number]
1 http://www.arduino.cc/en/Hacking/Libraries 2 http://www.arduino.cc/en/Tutorial/Pushbutton
[00-‐2] Getting Started – Ardu-‐ez
University of Tasmania 4
Switch Source Code /* Switch Library - SwitchSerial */ /* ===== Switch Method ===== void begin(); // Waiting pushed key. if push key, return keyNum, else return 0; uint8_t getPushKey(); uint8_t waitGetPushKey(); =========================== */ #include "Switch.h" //include “Switch.h” to use the library for switch Switch pushSwitch; //call class “Switch” and named as pushSwitch void setup() { pushSwitch.begin(); //initialise Switch module Serial.begin(9600); //initialise Serial Monitor }
void loop() { /*assign the variable pushKey as a pressed button number, otherwise the pushKey will be assigned as 0 (Please refer to Switch module material).*/ uint8_t pushKey = pushSwitch.waitGetPushKey(); if(pushKey > 0) { // if pushKey is higher than 0 (any button is pressed), Serial.print("Pushed : "); // print out “Pushed : “ Serial.println(pushKey); // print out the pressed button number } }
Verify and upload the codes, then it will give an output in the Serial Monitor like below:
[00-‐2] Switch
[00-‐2] Getting Started – Ardu-‐ez
University of Tasmania 5
(?)
(?)
Try to change the code (highlighted part): void loop() { /*assign the variable pushKey as a pressed button number, otherwise the pushKey will be assigned as 0 (Please refer to Switch module material).*/ uint8_t pushKey = pushSwitch.waitGetPushKey(); if(pushKey > 0) { // if pushKey is higher than 0 (any button is pressed), Serial.print("Pushed : "); // print out “Pushed : “ Serial.println(pushKey); // print out the pressed button number } } to: void loop() { /*assign the variable pushKey as a pressed button number, otherwise the pushKey will be assigned as 0 (Please refer to Switch module material).*/ uint8_t pushKey = pushSwitch.waitGetPushKey(); if(pushKey > 8) { // if pushKey is higher than 0 (any button is pressed), Serial.print("Pushed : "); // print out “Pushed : “ Serial.println(pushKey); // print out the pressed button number } } and then, verify and upload the codes. The serial monitor will display the pushed switch number when the switch number is higher than 8.
[CHALLENGE] Try to combine two programs Blink and Switch. Fill the blanks in the following code to turn the LED ON / OFF using switch. Use button number 1 to turn ON, number 2 to turn OFF. Code: #include "Switch.h" //include “Switch.h” to use the library for switch Switch pushSwitch; //call class “Switch” and named as pushSwitch void setup() { pushSwitch.begin(); //initialise Switch module Serial.begin(9600); //initialise Serial Monitor pinMode(13, OUTPUT); //set the pin number 13 for LED } void loop() { /*assign the variable pushKey as a pressed button number, otherwise the pushKey will be assigned as 0 (Please refer to Switch module material).*/ uint8_t pushKey = pushSwitch.waitGetPushKey();
if(pushKey == ){ // if pushKey is 1 (button number 1 is pressed),
Serial.print("Turn the LED on"); // print out “Turn the LED on “
// turn the LED ON
} else if(pushKey == ){ // if pushKey is 2 (button number 2 is pressed), Serial.print("Turn the LED off"); // print out “Turn the LED on “
// turn the LED OFF
} }
(?)
(?)
[00-‐2] Switch_Challenge
[00-‐2] Getting Started – Ardu-‐ez
University of Tasmania 6
PIR Sensor
PIR sensors allow you to sense motion, almost always used to detect whether a human has moved in or out of the sensors range. They are small, inexpensive, low-‐power, easy to use and don't wear out. For that reason they are commonly found in appliances and gadgets used in homes or businesses. They are often referred to as PIR, "Passive Infrared", "Pyroelectric", or "IR motion" sensors.
PIR Sensor Source Code /* ===== PIR Method ===== void begin(uint8_t pinNum); int readValue(); ======================== */ #include "PIR.h" //include “PIR.h” to use the library for PIR #define PIR_PIN 39 //define PIN number for PIR sensor PIR pir; //call class “pir” and named as PIR void setup() { pir.begin(PIR_PIN); //initialise PIR sensor Serial.begin(9600); //initialise Serial Monitor } void loop() { Serial.println(pir.readValue()); //print out the value of PIR Sensor delay(1000); //delay 1000 milliseconds (1 second) }
Verify and upload the codes, then it will give an output in the Serial Monitor like below:
[00-‐2] PIR
[00-‐2] Getting Started – Ardu-‐ez
University of Tasmania 7
(?)
Try to change the code: void loop() { Serial.println(pir.readValue()); //print out the value of PIR Sensor delay(1000); //delay 1000 milliseconds (1 second) }
to: void loop() { if((pir.readValue()==1){ //if the PIR value is 1, execute the following… Serial.println(“Detected”); //print out the string “Detected” } delay(1000); //delay 1000 milliseconds (1 second) }
and then, verify and upload the codes. The serial monitor will display “Detected” when the PIR sensor module detects when a human has moved in or out of the sensors range.
[CHALLENGE] Try to combine two programs Blink and PIR. Fill the blanks in the following code to turn the LED ON / OFF using switch. Turn the LED ON when the PIR sensor gets the value 1, and turn the LED OFF when the PIR sensor gets the value 0. Code: #include "PIR.h" //include “PIR.h” to use the library for PIR #define PIR_PIN 39 //define PIN number for PIR sensor PIR pir; //call class “pir” and named as PIR void setup() { pir.begin(PIR_PIN); //initialise PIR sensor Serial.begin(9600); //initialise Serial Monitor pinMode(13, OUTPUT); //set the pin number 13 for LED } void loop() { if((pir.readValue()) == ){ // if the PIR value is 1 Serial.print("Turn the LED on"); // print out “Turn the LED on “
// turn the LED ON
} else if((pir.readValue()) == ){ // if the PIR value is 0
Serial.print("Turn the LED off"); // print out “Turn the LED on “
// turn the LED OFF
} }
(?)
(?)
[00-‐2] PIR_Challenge
[00-‐2] Getting Started – Ardu-‐ez
University of Tasmania 8
How to do Troubleshoot3 There will come a moment in your experimentation when something will stop working and you will have to figure out how to fix it. Troubleshooting and debugging are ancient arts in which there are a few simple rules, but most of the results are obtained through a lot of work. As every Arduino-‐based project is made up of both hardware and software, there will be more than one place to look if something goes wrong. While looking for a bug, you should operate along three lines:
1) Understanding
Try to understand as much as possible how the parts that you’re using work and how they’re supposed to contribute to the finished project. This approach will allow you to devise some ways to test each component separately.
2) Simplification and segmentation
Try to break down (mentally) the project into its components by using the understanding you have and figure out where the responsibility of each component begins and ends.
3) Exclusion and certainty
While investigating, test each component separately so that you can be absolutely certain that each one works by itself. You will gradually build up confidence about which parts of project are doing their job and which ones are dubious.
3 Get Started with Arduino (Massimo Banzi)