input modules for arduino

9

Click here to load reader

Upload: hairforceone

Post on 23-Oct-2015

25 views

Category:

Documents


2 download

DESCRIPTION

Arduino Input Modules

TRANSCRIPT

Page 1: Input Modules for Arduino

 

           [00-­‐3]  Input  Modules      

 

     

Smart  Services  Systems,  University  of  Tasmania    

 

PIR  Sensor  

Ultrasonic  Sensor    

Gas  Sensor  

Temperature  Sensor  

Switch  

Page 2: Input Modules for Arduino

[00-­‐3]  Input  Modules  

                               University  of  Tasmania   2  

Introduction  We  have  covered  very  basic  Arduino  programming  last  session.  In  this  session,  we  will  learn  input  modules.  There  are  9  input  modules  in  the  Ardu-­‐ez  box:    

• PIR  Sensor  Module  • Switch  Module  • Ultrasonic  Sensor  Module  • Gas  Sensor  Module  • Humidity  Sensor  Module  • Sound  Sensor  Module  • Light  Sensor  Module  • Temperature  Sensor  Module  • 3-­‐Axis  Gyroscope  Sensor  Module  

 We  will  use  the  RGBLCD  to  display  the  values  received  from  input  modules.  Let’s  see  what  is  RGBLCD  and  what  can  be  done  with  RGBLCD  first.  

RGBLCD  The  RGBLCD  module  is  connected  to  Ardu-­‐ez  board.  There  are  total  14  pins  (pin  number   42~49   &   62~67)   are   connected   to   RGBLCD   module.   The   RGBLCD  module  displays  total  32  characters  (2  row  x  16  columns).    

 

Let’s   try   the   sample   code   first.   The   sample   code   can   be   downloaded   from   the  website:    

http://www.cis.utas.edu.au/iweb3/arduino  (“Download  Arduino  Sample  Codes”  >  “[00-­‐3]  Input  Modules”  >  “1.  RGBLCD”)  

Copy  and  paste  the  code  to  the  sketch.  

Page 3: Input Modules for Arduino

[00-­‐3]  Input  Modules  

                               University  of  Tasmania   3  

Source  Code  The  provided  code  will  display  the  string  “Hello  World”  and  change  the  colour  of  the  backlight  (red,  green  and  blue).    /* RgbLcd Library - Hello World RgbLcd Library used LiquidCrystal Library http://www.arduino.cc/en/Tutorial/LiquidCrystal */ /* ===== RgbLcd Method ===== void begin(uint8_t rs, uint8_t rw, uint8_t enable, uint8_t r, uint8_t g, uint8_t b, uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t cols=16, uint8_t rows=2); void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS); void clear(); void home(); void setCursor(uint8_t, uint8_t); void onBacklight(uint8_t rgb, uint8_t bOn); void onBacklightRed(uint8_t bOn=1); void onBacklightGreen(uint8_t bOn=1); void onBacklightBlue(uint8_t bOn=1); =========================== */ /* ===== IMPORT LIBRARY FILE ===== */ #include "RgbLcd.h" /* ================================ */ /* ===== Define Pin Number ===== */ #define RS_PIN 62 #define RW_PIN 63 #define E_PIN 64 #define R_PIN 65 #define G_PIN 66 #define B_PIN 67 #define DATA_PIN4 45 #define DATA_PIN5 44 #define DATA_PIN6 43 #define DATA_PIN7 42 /* ================================ */ /* ===== Define RGBLCD size ===== */ #define LCD_COL 16 #define LCD_ROW 2 /* ================================ */ //Declaration RgbLcd class RgbLcd lcd; //Declaration displayString variable String displayString; void setup(){ // initialise LCD module and set up the LCD's number of columns and rows lcd.begin(RS_PIN, RW_PIN, E_PIN, R_PIN, G_PIN, B_PIN, DATA_PIN4, DATA_PIN5, DATA_PIN6, DATA_PIN7, LCD_COL, LCD_ROW); displayString = "Hello World"; }

These  are  the  functions  in  the  RGBLCD  library  

Page 4: Input Modules for Arduino

[00-­‐3]  Input  Modules  

                               University  of  Tasmania   4  

void loop() { /* ========================= Red Backlight ========================= */ // turn the blue backlight OFF lcd.onBacklightBlue(0); // can be replaced with -> lcd.onBacklight(2,0); // turn the red backlight ON lcd.onBacklightRed(); // can be replaced with -> lcd.onBacklight(0,1); lcd.clear(); // clear the RGBLCD lcd.print(displayString); // print the string assigned in the setup() lcd.setCursor(0,1); // set the cursor in the second row of the RGBLCD lcd.print("Red"); // print out the "Red" delay(1000); // delay for one second /* ================================================================== */ /* ========================= Green Backlight ========================= */ // turn the red backlight OFF lcd.onBacklightRed(0); // can be replaced with -> lcd.onBacklight(0,0); // turn the green backlight ON lcd.onBacklightGreen(); // can be replaced with -> lcd.onBacklight(1,1); lcd.clear(); // clear the RGBLCD lcd.print(displayString); // print the string assigned in the setup() lcd.setCursor(0,1); // set the cursor in the second row of the RGBLCD lcd.print("Green"); // print out the "Green" delay(1000); // delay for one second /* ================================================================== */ /* ========================= Blue Backlight ========================= */ // turn the green backlight OFF lcd.onBacklightGreen(0); // can be replaced with -> lcd.onBacklight(1,0); // turn the blue backlight ON lcd.onBacklightBlue(); // can be replaced with -> lcd.onBacklight(2,1); lcd.clear(); // clear the RGBLCD lcd.print(displayString); // print the string assigned in the setup() lcd.setCursor(0,1); // set the cursor in the second row of the RGBLCD lcd.print("Blue"); // print out the "Green" delay(1000); // delay for one second /* ================================================================== */ }

Activity  Try  to  change  the  String    (highlighted)  part  to:   displayString = "Any other string you want to put";

 Then,  upload  the  code  to  the  Arduino  board  and  see  the  difference.      We  have  learned  RGBLCD  module.  Now,  let’s  try  Ultrasonic  Sensor  module  (one  of  the  input  modules).  

Page 5: Input Modules for Arduino

[00-­‐3]  Input  Modules  

                               University  of  Tasmania   5  

Ultrasonic  Sensor  Module  

 The  ultrasonic  sensor  module  is  connected  to  Ardu-­‐ez  board.  Only  one  pin  (pin  number   75)   is   connected   to   ultrasonic   sensor   module.   The   ultrasonic   sensor  module  generates  high  frequency  sound  waves  and  evaluates  the  echo,  which  is  received  back  by  the  sensor.  

Source  Codes  The  sample  code  will  display  the  distance  between  the  ultrasonic  sensor  module  and  the  object  in  millimetre.  

The  sample  code  can  be  downloaded  from  the  website:    

http://www.cis.utas.edu.au/iweb3/arduino  (“Download  Arduino  Sample  Codes”  >  “[00-­‐3]  Input  Modules”  >  “2.  Ultrasonic  Sensor”)  

Copy  and  paste  the  code  to  the  sketch.   /* UltraSonic Library */ /* ===== UltraSonic Method ===== void begin(int pinNum); int ReadDistanceMilimeter(); // mm int ReadDistanceCentimeter(); // cm int ReadDistanceInche(); // inch =============================== */ #include "UltraSonic.h" // include ultrasonic sensor module library #define ULTRA_PIN 75 // define the pin number of the module UltraSonic ultra; // declare the class UltraSonic as ultra int UltraDistance; // declare the variable (integer) UltraDistance void setup() { ultra.begin(ULTRA_PIN); // set the begin function of ultrasonic module Serial.begin(9600); // set the begin function of Serial monitor(9600) } void loop() { // assign the distance between the module and the object as UltraDistance UltraDistance = ultra.ReadDistanceMilimeter(); // display variable UltraDistance. Serial.print(UltraDistance); // display the “mm” Serial.println("mm"); // delay 1 second delay(1000); }

Page 6: Input Modules for Arduino

[00-­‐3]  Input  Modules  

                               University  of  Tasmania   6  

Activity  Try  to  change  the  loop  (highlighted)  part  to:   // assign the distance between the module and the object as UltraDistance UltraDistance = ultra.ReadDistanceCentimeter(); // display variable UltraDistance. Serial.print(UltraDistance); // display the “cm” Serial.println("cm"); // delay 1 second delay(1000);

Page 7: Input Modules for Arduino

[00-­‐3]  Input  Modules  

                               University  of  Tasmania   7  

Combine  Modules  We  can  use  both  modules  in  the  same  time.  Before  we  look  into  the  combined  source  code,  we  need  to  understand  which  parts  are  necessary  to  include  in  the  code.  For  most  cases,  the  following  components  are  necessary:  

1. Import  library  part  2. Define  the  pin  number  3. Declare  the  class  of  the  module  /  declare  the  variable  4. Set  the  begin  function  in  setup()  

 /* UltraSonic Library */ /* ===== UltraSonic Method ===== void begin(int pinNum); int ReadDistanceMilimeter(); // mm int ReadDistanceCentimeter(); // cm int ReadDistanceInche(); // inch =============================== */ #include "UltraSonic.h" // include ultrasonic sensor module library #define ULTRA_PIN 75 // define the pin number of the ultrasonic module UltraSonic ultra; // declare the class UltraSonic as ultra int UltraDistance; // declare the variable (integer) UltraDistance void setup() { ultra.begin(ULTRA_PIN); // set the begin function of ultrasonic module Serial.begin(9600); // set the begin function of Serial monitor(9600) } void loop() { // assign the distance between the module and the object as UltraDistance UltraDistance = ultra.ReadDistanceMilimeter(); // display variable UltraDistance. Serial.print(UltraDistance); // display the “mm” Serial.println("mm"); // delay 1 second delay(1000); }

 *loop  function  part  may  vary  depending  on  the  program.    Then,  go  to  the  website  and  open  the  code  “Combine_RGBLCD_Ultra.ino”  that  you  can  download  from  website.  

http://www.cis.utas.edu.au/iweb3/arduino  (“Download  Arduino  Sample  Codes”  >  “[00-­‐3]  Input  Modules”  >  “4.  Combine_RGBLCD_Ultra”)    

*Comments  

1.  import  library  

2.  define  the  pin  number  

3.  declare  the  class  /  variable  

4.  set  the  begin  function  in  setup()  

Page 8: Input Modules for Arduino

[00-­‐3]  Input  Modules  

                               University  of  Tasmania   8  

Activity  Fill   the   blank  below  and   try   to   complete   the   code.   The   completed   code   should  display   the   “Ultrasonic   Distance   =   ”   with   the   measured   distance   between   the  object  and  the  module.  The  measured  distance  should  be  updated  every  second.      /* ===== IMPORT LIBRARY FILE ===== */ #include "RgbLcd.h" // include RGBLCD module library

// include ultrasonic sensor module library /* ================================ */ /* ===== Define Pin Number ===== */ #define RS_PIN 62 #define RW_PIN 63 #define E_PIN 64 #define R_PIN 65 #define G_PIN 66 #define B_PIN 67 #define DATA_PIN4 45 #define DATA_PIN5 44 #define DATA_PIN6 43 #define DATA_PIN7 42 // define the pin number of the ultrasonic module /* ================================ */ /* ===== Define LCD size ===== */ #define LCD_COL 16 #define LCD_ROW 2 /* ================================ */ //Declaration RgbLcd class RgbLcd lcd; // declare the class UltraSonic as ultra // declare the variable (integer) UltraDistance void setup(){ // initialise LCD module and set up the LCD's number of columns and rows lcd.begin(RS_PIN, RW_PIN, E_PIN, R_PIN, G_PIN, B_PIN, DATA_PIN4, DATA_PIN5, DATA_PIN6, DATA_PIN7, LCD_COL, LCD_ROW); // turn the green backlight ON lcd.onBacklightGreen(); // can be replaced with -> lcd.onBacklight(1,1); // set the begin function of ultrasonic module } void loop() { // assign the distance between the module and the object as UltraDistance

lcd.clear(); // clear the RGBLCD lcd.print("Ultrasonic"); // print out the "Ultrasonic" lcd.setCursor(0,1); // set the cursor in the second row of the RGBLCD lcd.print("Distance = "); // print out the "Distance = " // print the variable UltraDistance delay(1000); // delay for one second }

//FILL HERE //

//FILL HERE //

//FILL HERE //

//FILL HERE //

//FILL HERE //

//FILL HERE //

//FILL HERE //

Page 9: Input Modules for Arduino

[00-­‐3]  Input  Modules  

                               University  of  Tasmania   9  

Other  Input  Modules  There  are  more  input  modules  materials  on  the  website:  

http://www.cis.utas.edu.au/iweb3/arduino  (“Download  Arduino  Materials”  >  “2.  Modules”  >  “Input  Modules”)    You  can  try  some  code  from  the  materials  and  try  to  combine  them  with  RGBLCD.  You  may  start  from  “Gas  Sensor  Module”.    Input  Modules:    

• PIR  Sensor  Module  • Switch  Module  • Ultrasonic  Sensor  Module  • Gas  Sensor  Module  • Humidity  Sensor  Module  • Sound  Sensor  Module  • Light  Sensor  Module  • Temperature  Sensor  Module  • 3-­‐Axis  Gyroscope  Sensor  Module