1.Arduino Nano V 3.1 Arduino Nano เป็ นบอร์ ดไมโครคอนโทรลเลอร์ ที่มีขนาดเล็ก ที่สามารถใช้งานร่ วมกับโปรโตบอร์ ด (protoboard) ใช้ไมโครคอนโทรลเลอร์ ATmega328 ในการประมวลผล มีฟังก์ชนั การท างานเหมือนกัน Arduino รุ่ นอื่นๆ ใช้สาย USB mini ในการโปรแกรม ผลิตโดยบริ ษทั กราวิเทคไทย (ไทยแลนด์) จากัด Arduino Nano Pins Input and Output ขาดิจิตอลทั้ง 14 ขาของ Arduino Nano สามารถใช้เป็ นได้ท้ งั ขา input หรื อขา output โดยใช้ ฟังก์ชนั pinMode(); digitalRead(); และ digitalWrite(); ขาดิจิตอลแต่ละขาท างานที่ 5V และจ่ายกระแสได้ สู งสุ ด 40 mA และมี internal pull-up resistor (ถูกปิ ดการท างานในภาวะปกติ) นอกจากนั้นแต่ละขายังมี ความสามารถพิเศษอื่นๆ ดังนี้ Serial Communication ขา RX0 และขา TX1 ใช้เป็ น ขาส าหรับ รับข้อมูล (RX) และส่ งข้อมูล (TX) ตามล าดับทั้งสองขานี้เชื่ อมต่อกับชิป FTDI ซึ่ งเป็ นชิปส าหรับแปลงการเชื่อมต่อ แบบ USB ให้เป็ น serial External interrupt ขา D2 และ ขา D3 สามารถเขียนโปรแกรมให้เป็ น ขาส าหรับรับสัญญาณ Interupt จากภายนอก ด้วยการใช้ฟังก์ชนั่ attachinterrupt(); PWM ขา D3, D5, D6, D9, D10 และ D11 สามารถใช้เป็ นขาส าหรับส่ งสัญญาณ PWM (8 bit) ด้วยฟังก์ชนั analogWrite(); SPI ขา D10 (SS), ขา D11 (MOSI), ขา D12 (MISO) และขา D13 (SCK) ทั้งสี่ ขานี้ รองรับการสื่ อสารแบบ SPI On-board LED บน Arduino Nano มี LED อยูบ่ นบอร์ ดที่ต่อโดยตรงกับขา D13 สามารถ เขียนโปรแกรมเพื่อควบคุมการสว่างของ LED ADC Arduino Nano มีขาส าหรับแปลงสัญญาณ analog ให้เป็ น สัญญาณ digital อยูท่ ้ งั หมด 8 ขา คือขา A0 - A7 สามารถแปลงสัญญาณได้ละเอียด 10 bit (1024 ระดับ) โดยใช้ฟังก์ชนั analogRead(); โดยปกติแล้วในการแปลง สัญญาณจะเปรี ยบเทียบจาก ground ถึง 5V และสามารถเปลี่ยนช่วงของการ เปรี ยบเทียบค่าได้ดว้ ยฟังก์ชนั่ analogReference(); หมายเหตุ : pin A6 และ A7 ไม่สามารถใช้เป็ นขา digital I2C A4 (SDA) และ A5 (SCL) สนับสนุนการสื่ อสารแบบ I2C Analog reference (AREF) AREF เป็ นขาส าหรับต่อแรงดันอ้างอิงให้กบั การอ่านค่า analog ใช้งานร่ วมกับ ฟังก์ชนั analogReference(); Reset pin เป็ นขาส าหรับต่อวงจร เพื่อรี เซตไมโครคอนโทรลเลอร์ นอกเหนื อจากปุ่ มรี เซต เดิมที่มีอยูบ่ นบอร์ ด และเมื่อขา reset เป็ นลอจิก 0 ไมโครคอนโทรลเลอร์จะท า การรี เซตตัวเอง 2.Bidirectional l2C Voltage-Level Translator(USA) เป็ นบอร์ดที่ใช้สาหรับปรับแรงดันของขา input และ output จาก 5 V ให้เป็ น 3.3 V แบบ 2 ทาง เพื่อให้สามารถติดต่อกับโมดูลเซนเซอร์ อื่นๆ ที่ใช้แรงดัน 3.3 V ในโมดูลปรับแรงดันที่มีวงจร 3.3V 250 mA voltage regulator สามารถใช้เป็ นแหน่งจ่ายให้กบั วงจรภายนอกได้ เข้ากันได้กบั การสื่ อสารแบบ I2C ในโหมด Standard(10 kbit/s) และ l2C โหมด Fast(400 kbit/s)ที่มีสัญญาณรบกวนต่า และสามารถเสี ยบลงบนบอร์ ดได้ 3.เซนเซอร์ วดั อุณหภมิและความชื้น เป็ นโมดูลเซนเซอร์ วดั อุณหภูมิและความชื้นที่ใช้ ไอซี Si7005-B-GM1R ใช้การสื อสารด้วย l2C มีขนาดเล็ก ใช้พลังงานต่า (240 ไมโครแอมป์ ) สามารถเสี ยบลงบอร์ดทดลองเพื่อทดลอง Library นี้ใช้ส าหรับอ่านค่าความชื้นและอุณหภูมิจากเซนเซอร์ I2CHUTMPv1.0 มี 4 ขาจากบอร์ ดใช้ การเชื่ อมต่อแบบ I2C ขีดความสามารถในการวัดอุณหภูมิอยูท่ ี่ –40 ถึง +85 องศาเซลเซียส คลาดเคลื่อนไม่ เกิน 0.5 องศาเซลเซี ยส และ ขีดความสามารถในการวัดความชื้นสัมพัทธ์อยูท่ ี่ 0 ถึง 100 เปอร์ เซ็นต์ คลาดเคลื่อนไม่เกิน 3 เปอร์เซ็นต์ พร้อมโหมดความเร็ วสู งเพื่อให้วเิ คราะห์ขอ้ มูลเร็ วขึ้นและโหมดไล่ความชื้น หน้าเซนเซอร์ เพื่อให้การอ่านค่าความชื้นสัมพัทธ์มีความแม่นย ามากขึ้น 1. Vcc แรงดัน 3.3v 2. Gnd 3. SDA สาหรับ Nano A4 ใช้แรงดัน 3.3v 4. SCL สาหรับ Nano A5 ใช้แรงดัน 3.3v Library : 3.Gtech_I2CHUTMP EX. Code ทาหน้ าทีว่ ดั อุณหภมิในห้ อง #include <Gtech_Si7005.h> #include <Wire.h> Gtech_Si7005 read1; void setup() { Serial.begin(9600); Serial.println( read1.setup()); } void loop() { float tmp; int tmp2; tmp2 = read1.getRawTemp(); Serial.print("room temperature is "); Serial.print(tmp2); Serial.print(" celcius\n"); delay(1000); tmp = read1.getTemp('c'); Serial.print("room temperature is "); Serial.print(tmp); Serial.print(" celcius\n"); delay(1000); tmp2 = read1.getRawRH(); Serial.print("room %RH is "); Serial.print(tmp2); Serial.print("\n"); delay(1000); tmp = read1.getRH(); Serial.print("room %RH is "); Serial.print(tmp); Serial.print("\n"); delay(1000); read1.fastModeOn(false); read1.heaterModeOn(false); } 4.เซนเซอร์ อ่านค่ าความเข้ มแสง ใช้สาหรับการติดต่อกับเซนเซอร์ RGB 16-bit Color Sensor หรื อ I2C-COLOR v1.0 ใช้ การเชื่อมต่อแบบ I2C โดยสามารถวัดค่าสี ได้ความละเอียดสู งสุ ดถึง 16-bit โดยมีฟังก์ชนั่ ปรับเพิม่ ค่าความ ละเอียดได้ตามต้องการแม้จะอยูใ่ นที่แสงน้อย และยังมีฟังก์ชนั่ เสริ มคือฟังก์ชนั่ จับเวลาเปิ ดเซ็นเซอร์ และ ฟังก์ชนั่ ส าหรับการจ่ายสัญญาณ Interrupt เพื่อส าหรับใช้ในการควบคุมเซนเซอร์ ข้ นั สู งอีกด้วย จานวนขาของอุปกรณ์มีท้ งั หมด 6 ขา 1. Vcc ใช้แรงดัน 3.3v 2. Gnd 3. SDA สาหรับ Nano A4 ใช้แรงดัน 3.3v 4. SCL สาหรับ Nano A5 ใช้แรงดัน 3.3v 5. SYNC สาหรับการจ่ายพัลซ์เพื่อใช้ร่วมกับฟังก์ชนั่ จับเวลา Library Folder : Four_Gtech_I2CCOLOR Ex. Code วัดความเข้มของแสงสี ต่างๆ #include <Wire.h> //must be included Gtech_TCS3414FN color1; unsigned int red,green,blue,cle; void setup() { color1.powerOn(1); //sensor and adc on color1.setupTimer(); //free run 12ms } void loop() { red=color1.getRed(); //get data red channel green=color1.getGreen(); //get data green channel blue=color1.getBlue(); //get data blue channel cle=color1.getClear(); //get data luminousity Serial.print("Red : "); Serial.println(red); Serial.print("Green : "); Serial.println(green); Serial.print("Blue : "); Serial.println(blue); Serial.print("Luminousity : "); Serial.println(cle); delay(1000); } 5. Ethernet With MicroSD add-on for Arduino Nano-W5100 Ethernet with MicroSD add-on for Arduino Nano โดยเรา สามารถเชือมต่อ internet และ สามารถเปิ ดไฟล์ใน microSD card ได้ ใช้การเชื่อมต่อแบบ SPI ทั้ง 2 โมดูล จานวนขาของอุปกรณ์มีท้ งั หมด 5 ขา 1. MOSI ส าหรับ Nano D11 2. MISO ส าหรับ Nano D12 3. SCK ส าหรับ Nano D13 4. SS ของ Ethernet D10 5. SS ของ microSD D4 Library Folder : Five_Ethernet EX. Code เช็ค Mac IP #include <SPI.h> #include <Ethernet.h> // Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // if you don't want to use DNS (and reduce your sketch size) // use the numeric IP instead of the name for the server: //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) char server[] = "www.google.com"; // name address for Google (using DNS) // Set the static IP address to use if the DHCP fails to assign IPAddress ip(192,168,0,177); // Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): EthernetClient client; void setup() { // Open serial communications and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for Leonardo only } // start the Ethernet connection: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // no point in carrying on, so do nothing forevermore: // try to congifure using IP address instead of DHCP: Ethernet.begin(mac, ip); } // give the Ethernet shield a second to initialize: delay(1000); Serial.println("connecting..."); // if you get a connection, report back via serial: if (client.connect(server, 80)) { Serial.println("connected"); // Make a HTTP request: client.println("GET /search?q=arduino HTTP/1.1"); client.println("Host: www.google.com"); client.println("Connection: close"); client.println(); } else { // kf you didn't get a connection to the server: Serial.println("connection failed"); } } void loop() { // if there are incoming bytes available // from the server, read them and print them: if (client.available()) { char c = client.read(); Serial.print(c); } // if the server's disconnected, stop the client: if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); // do nothing forevermore: while(true); } } 6. 16*2 White on Blue Character 16x2 White on Blue Character LCD with Backlight โดย จอ LCD นี้ใช้งานที่ แรงดันไฟ 5V เป็ นการเชื่อมต่อปกติถา้ ไม่ใช้ Serial character LCD driver ช่วยสามารถ ต่อได้ตาม file example ใน Library ได้เลย 1. LCD RS pin to digital pin 12 2. LCD Enable pin to digital pin 11 3. LCD D4 pin to digital pin 5 4. LCD D5 pin to digital pin 4 5. LCD D6 pin to digital pin 3 6. LCD D7 pin to digital pin 2 7. LCD R/W pin to ground 8. 10K potentiometer : ends to +5V and ground Library Folder : Six_LiquidCrystal EX. Code แสดง Hello World #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); // Print a message to the LCD. lcd.print("hello, world!"); } void loop() { // set the cursor to column 0, line 1 // (note: line 1 is the second row, since counting begins with 0): lcd.setCursor(0, 1); // print the number of seconds since reset: lcd.print(millis()/1000); } 7.Compass Magnetometer Sensor 3-Axis –HMC5883L ใช้สาหรับอ่านค่าสนามแม่เหล็กจากเซนเซอร์ GY271 มี 5 ขาจากบอร์ ดใช้การเชื่ อมต่อแบบ I2C ขีดความสามารถในการอ่านค่าสนามแม่เหล็กอยูท่ ี่ -8 ถึง +8 Gauss 1. Vcc แรงดัน 5V 2. Gnd 3. SDA สาหรับ Nano A4 4. SCL ส าหรับ Nano A5 5. DRDY บอกสถานะข้อมูล Library Folder : Seven_Gtech_I2CGY271 Ex.Code บอกทิศทาง #include <Gtech_HMC5883L.h> #include<Wire.h> Gtech_HMC5883L com1; float valfloat[3]; void setup() { Serial.begin(9600); Wire.begin(); com1.setup(); } void loop() { com1.getAllAxis(0,&valfloat[0]); Serial.print("Axis(millGauss) X = "); Serial.print(valfloat[0]); Serial.print(" Y = "); Serial.print(valfloat[1]); Serial.print(" Z = "); Serial.println(valfloat[2]); delay(500); } 8.Digital Acceleration of Gravity Tilt Module 3-Axis – ADXL345 เซนเซอร์ Accelerometer ADXL345 เชื่อมต่อแบบ I2C เป็ นเซนเซอร์ วดั ความเร่ ง 3 แกน ที่มีขนาดเล็ก และประหยัดพลังงาน ให้ผลลัพธ์ใน รู ปแบบดิจิตอล 16 บิต ADXL345 เหมาะกับอุปกรณ์พกพา สามารถวัดความเร่ งได้ท้ งั ที่คงที่และไม่คงที่ สามารถวัดได้ ด้วยความละเอียดถึง 4mg/LSB และยังประกอบด้วยฟังก์ชนั่ ที่หลากหลายที่สามารถเชื่อมต่อกับขา interrupt ซึ่งมี2 ขาให้เลือก วงจรภายในมี FIFO buffer ถึง 32 ระดับ สามารถใช้เก็บข้อมูลเพื่อลดภาระของเซนเซอร์ และยังมีฟังก์ชนั่ Low power mode ที่ช่วยประหยัดพลังงานอีกด้วย **ADXL345 ทางานได้ที่ระดับแรงดัน 2.0-3.6 V เท่านั้น ขาและการต่ออุปกรณ์ 1. GND ต่อเข้ากับ GND ของ Microcontroller 2.VCC ต่อเข้ากับ 3.3V 3. CS ต่อเข้ากับ 3.3V เพื่อเลือกใช้งาน I2C 4. INT1 5. INT2 6. SDO ตอ่เข้ากับ GND หรื อ 3.3V ก็ได้ เพื่อก าหนด Address 7. SDA ต่อเข้ากับขา SDA ของบอร์ดคอนโทรลเลอร์ (Analog 4 ส าหรับ Aduino Nano) 8. SCL ต่อเข้ากับขา SCL ของบอร์ดคอนโทรลเลอร์ (Analog 5 ส าหรับ Aduino Nano) Library Folder : Gtech_I2CGY291 Ex : code วัดองศา #include<stdio.h> #include<Wire.h> #include<Gtech_ADXL345.h> Gtech_ADXL345 acc1(1); int all[3]; //array to keep values x y z all[0]=x,all[1]=y,all[2]=z int x,y,z; //integer to keep the value of x,y,z void setup() { Serial.begin(115200); acc1.setup(); delay(1500); } void loop() { //get values by using getAll() acc1.getAll(all); Serial.print(all[0]); Serial.print(" "); Serial.print(all[1]); Serial.print(" "); Serial.println(all[2]); //get values by using getX(), getY(), getZ() /* x=acc1.getX(); y=acc1.getY(); z=acc1.getZ(); Serial.print(x); Serial.print(" "); Serial.print(y); Serial.print(" "); Serial.println(z); */ delay(100); } 9.ULN2003 Stepper Motor Control Board + 5V Stepper Motor ตัว stepper motor นี้ช่วยให้เราสามารถกาหนดองศาของการหมุนได้ ถือเป็ นประโยชน์อย่างมากหากต้องการเลื่อนอะไรแบบเจาะจงระยะทางหรื อองศา โดยตัวที่อยูช่ ุด ultimate sensor kit นี้จะมี specification ดังนี้ Model : 28BYJ-48 Rated voltage : 5VDC Number of Phase : 4 Speed Variation Ratio : 1/64 Stride Angle : 5.625° /64 Frequency : 100Hz DC resistance : 50Ω±7%(25℃) Friction torque : 600-1200 gf.cm Pull in torque : 300 gf.cm Insulated resistance : >10MΩ(500V) Insulated electricity power : 600VAC/1mA/1s Insulation grade : A Rise in Temperature : <40K(120Hz) Noise : <35dB(120Hz,No load,10cm) Library Folder : Stepper Ex : Code one step at a time #include <Stepper.h> const int stepsPerRevolution = 200; // change this to fit the number of steps per revolution // for your motor // initialize the stepper library on pins 8 through 11: Stepper myStepper(stepsPerRevolution, 8,9,10,11); int stepCount = 0; // number of steps the motor has taken void setup() { // initialize the serial port: Serial.begin(9600); } void loop() { // step one step: myStepper.step(1); Serial.print("steps:" ); Serial.println(stepCount); stepCount++; delay(500); } 10.I2C RTC DS1307 AT24C32 Real Time Clock Module สามารถตั้งค่าวันและเวลา ได้ตามที่ตอ้ งการ ใช้การเชื่อมต่อแบบ I2C ใช้ถ่านกระดุมรหัส LIA2032 3.6v มี register ที่ใช้เก็บ วินาที นาที ชัว่ โมง วัน วันที่ เดือน ปี สามารถเก็บเวลาได้สูงสุ ด ถึง 1 ศตวรรษ (ตามจานวนปี ที่ register เก็บได้) 1. Vcc 2. Gnd 3. SDA สาหรับ Nano A4 4. SCL สาหรับ Nano A5 Library Folder : Stepper 11.MAX7219 Dot LED Matrix Module สาหรับการควบคุม MAX7219 Dot matrix Module ให้แสดงผลตามที่เราต้องการได้ โดยมีฟังก์ชนั่ เสริ มคือโหมดประหยัดพลังงานและการตั้งความสว่างของ LED ด้วย ขาทั้งหมดของ MAX7219 Dot matrix Module มีท้ งั หมด 5 ขา ประกอบด้วย 1. Vcc แรงดัน 5v 2. Gnd 3. DIN ต่อเข้ากับ Digital PIN 12 4. CS ต่อเข้ากับ Digital PIN 10 5. CLK ต่อเข้ากับ Digital PIN 11 Library Folder : LedControl Ex : code LED Matrix #include "LedControl.h" /* Now we need a LedControl to work with. ***** These pin numbers will probably not work with your hardware ***** pin 12 is connected to the DataIn pin 11 is connected to the CLK pin 10 is connected to LOAD We have only a single MAX72XX. */ LedControl lc=LedControl(12,11,10,1); /* we always wait a bit between updates of the display */ unsigned long delaytime=100; void setup() { /* The MAX72XX is in power-saving mode on startup, we have to do a wakeup call */ lc.shutdown(0,false); /* Set the brightness to a medium values */ lc.setIntensity(0,8); /* and clear the display */ lc.clearDisplay(0); } /* This method will display the characters for the word "Arduino" one after the other on the matrix. (you need at least 5x7 leds to see the whole chars) */ void writeArduinoOnMatrix() { /* here is the data for the characters */ byte a[5]={B01111110,B10001000,B10001000,B10001000,B01111110}; byte r[5]={B00111110,B00010000,B00100000,B00100000,B00010000}; byte d[5]={B00011100,B00100010,B00100010,B00010010,B11111110}; byte u[5]={B00111100,B00000010,B00000010,B00000100,B00111110}; byte i[5]={B00000000,B00100010,B10111110,B00000010,B00000000}; byte n[5]={B00111110,B00010000,B00100000,B00100000,B00011110}; byte o[5]={B00011100,B00100010,B00100010,B00100010,B00011100}; /* now display them one by one with a small delay */ lc.setRow(0,0,a[0]); lc.setRow(0,1,a[1]); lc.setRow(0,2,a[2]); lc.setRow(0,3,a[3]); lc.setRow(0,4,a[4]); delay(delaytime); lc.setRow(0,0,r[0]); lc.setRow(0,1,r[1]); lc.setRow(0,2,r[2]); lc.setRow(0,3,r[3]); lc.setRow(0,4,r[4]); delay(delaytime); lc.setRow(0,0,d[0]); lc.setRow(0,1,d[1]); lc.setRow(0,2,d[2]); lc.setRow(0,3,d[3]); lc.setRow(0,4,d[4]); delay(delaytime); lc.setRow(0,0,u[0]); lc.setRow(0,1,u[1]); lc.setRow(0,2,u[2]); lc.setRow(0,3,u[3]); lc.setRow(0,4,u[4]); delay(delaytime); lc.setRow(0,0,i[0]); lc.setRow(0,1,i[1]); lc.setRow(0,2,i[2]); lc.setRow(0,3,i[3]); lc.setRow(0,4,i[4]); delay(delaytime); lc.setRow(0,0,n[0]); lc.setRow(0,1,n[1]); lc.setRow(0,2,n[2]); lc.setRow(0,3,n[3]); lc.setRow(0,4,n[4]); delay(delaytime); lc.setRow(0,0,o[0]); lc.setRow(0,1,o[1]); lc.setRow(0,2,o[2]); lc.setRow(0,3,o[3]); lc.setRow(0,4,o[4]); delay(delaytime); lc.setRow(0,0,0); lc.setRow(0,1,0); lc.setRow(0,2,0); lc.setRow(0,3,0); lc.setRow(0,4,0); delay(delaytime); } /* This function lights up a some Leds in a row. The pattern will be repeated on every row. The pattern will blink along with the row-number. row number 4 (index==3) will blink 4 times etc. */ void rows() { for(int row=0;row<8;row++) { delay(delaytime); lc.setRow(0,row,B10100000); delay(delaytime); lc.setRow(0,row,(byte)0); for(int i=0;i<row;i++) { delay(delaytime); lc.setRow(0,row,B10100000); delay(delaytime); lc.setRow(0,row,(byte)0); } } } /* This function lights up a some Leds in a column. The pattern will be repeated on every column. The pattern will blink along with the column-number. column number 4 (index==3) will blink 4 times etc. */ void columns() { for(int col=0;col<8;col++) { delay(delaytime); lc.setColumn(0,col,B10100000); delay(delaytime); lc.setColumn(0,col,(byte)0); for(int i=0;i<col;i++) { delay(delaytime); lc.setColumn(0,col,B10100000); delay(delaytime); lc.setColumn(0,col,(byte)0); } } } /* This function will light up every Led on the matrix. The led will blink along with the row-number. row number 4 (index==3) will blink 4 times etc. */ void single() { for(int row=0;row<8;row++) { for(int col=0;col<8;col++) { delay(delaytime); lc.setLed(0,row,col,true); delay(delaytime); for(int i=0;i<col;i++) { lc.setLed(0,row,col,false); delay(delaytime); lc.setLed(0,row,col,true); delay(delaytime); } } } } void loop() { writeArduinoOnMatrix(); rows(); columns(); single(); } 12.Ultrasonic Module HC-SR04 Distance Sensor Measuring Transducer สาหรับการควบคุม Ultrasonic Sensor Module ให้อ่านค่าระยะทาง โดยการใช้คลื่นเสี ยง Ultrasonic ในการวัดระยะทาง มีระยาการอ่านค่าได้สูงสุ ด 4 เมตร และต่าสุ ด 0.02 เมตร ขาทั้งหมดของ Ultrasonic Sensor Module มีท้ งั หมด 4 ขา ประกอบด้วย 1. Vcc แรงดัน 5v 2. Gnd 3. Trig (Trigger) ต่อกับ Digital PIN 4. Echo ต่อกับ Digital PIN Library Folder : NewPing Ex : code วัดระยะทาง #include <NewPing.h> #define TRIGGER_PIN 12 #define ECHO_PIN 11 #define MAX_DISTANCE 200 NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance. void setup() { Serial.begin(9600); } void loop() { delay(50); unsigned int uS = sonar.ping_cm(); Serial.print(uS); Serial.println(“cm”); } 13.Mifare RFID RC522 Reader IC Card Module Tags SPI Interface Read and Write สาหรับการควบคุม Mifare RFID Module สาหรับการอ่านบัตรระบุตวั ตนด้วยระบบไร้สาย (RFID) สามารถทาได้ท้ งั การอ่านข้อมูลจากบัตรและเขียนข้อมูลลงบนตัวบัตร โดยการเขียนและอ่านจะใช้การเข้ารหัสแบบ Crypto1 ขาทั้งหมดของ Mifare RFID Module มีท้ งั หมด 8 ขา ประกอบด้วย 1. Vcc แรงดัน 3.3v 2. Ground 3. SDA (Alternate SS : Slave Select) ต่อกับ Digital PIN 10 4. SCK (Clock) ต่อกับ Digital PIN 13 5. MOSI (Master in Slave out) ต่อกับ Digital PIN 11 6. MISO (Master out Slave in) ต่อกับ Digital PIN 12 7. IRQ ขาเปล่า ไม่ได้ใช้งาน 8. RST (Reset PIN) ต่อกับ Digital PIN 9 (เปลี่ยนแปลงได้) Library Folder : rfidmaster Ex : Code Read and Write #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 9 MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance. void setup() { Serial.begin(9600); SPI.begin(); mfrc522.PCD_Init(); // Initialize serial communications with the PC // Init SPI bus // Init MFRC522 card //Serial.println("Scan a MIFARE Classic PICC to demonstrate Value Blocks."); } void loop() { // Prepare key - all keys are set to FFFFFFFFFFFFh at chip delivery from the factory. MFRC522::MIFARE_Key key; for (byte i = 0; i < 6; i++) { key.keyByte[i] = 0xFF; } // Look for new cards if ( ! mfrc522.PICC_IsNewCardPresent()) { return; } // Select one of the cards if ( ! mfrc522.PICC_ReadCardSerial()) { return; } // Now a card is selected. The UID and SAK is in mfrc522.uid. // Dump UID Serial.print("Card UID:"); for (byte i = 0; i < mfrc522.uid.size; i++) { Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(mfrc522.uid.uidByte[i], HEX); } Serial.println(); // Dump PICC type byte piccType = mfrc522.PICC_GetType(mfrc522.uid.sak); Serial.print("PICC type: "); Serial.println(mfrc522.PICC_GetTypeName(piccType)); if ( piccType != MFRC522::PICC_TYPE_MIFARE_MINI && piccType != MFRC522::PICC_TYPE_MIFARE_1K && piccType != MFRC522::PICC_TYPE_MIFARE_4K) { //Serial.println("This sample only works with MIFARE Classic cards."); return; } // In this sample we use the second sector (ie block 4-7). the first sector is = 0 // scegliere settore di lettura da 0 = primo settore byte sector = 1; // block sector 0-3(sector0) 4-7(sector1) 8-11(sector2) // blocchi di scrittura da 0-3(sector0) 4-7(sector1) 8-11(sector2) byte valueBlockA = 4; byte valueBlockB = 5; byte valueBlockC = 6; byte trailerBlock = 7; byte status; // Authenticate using key A. // avvio l'autentificazione A //Serial.println("Authenticating using key A..."); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_A, trailerBlock, &key, &(mfrc522.uid)); if (status != MFRC522::STATUS_OK) { Serial.print("PCD_Authenticate() failed: "); Serial.println(mfrc522.GetStatusCodeName(status)); return; } // Authenticate using key B. // avvio l'autentificazione B //Serial.println("Authenticating again using key B..."); status = mfrc522.PCD_Authenticate(MFRC522::PICC_CMD_MF_AUTH_KEY_B, trailerBlock, &key, &(mfrc522.uid)); if (status != MFRC522::STATUS_OK) { Serial.print("PCD_Authenticate() failed: "); Serial.println(mfrc522.GetStatusCodeName(status)); return; } // Writing new value block A // Scrivo i valori per il settore A Serial.println("Writing new value block A(4) : the first of the sector TWO "); byte value1Block[] = { 1,2,3,4, 5,6,7,8, 9,10,255,12, 13,14,15,16, valueBlockA,~valueBlockA,valueBlockA,~valueBlockA }; status = mfrc522.MIFARE_Write(valueBlockA, value1Block, 16); if (status != MFRC522::STATUS_OK) { Serial.print("MIFARE_Write() failed: "); Serial.println(mfrc522.GetStatusCodeName(status)); } /* // Writing new value block B // Scrivo i valori per il settore B Serial.println("Writing new value block B"); byte value2Block[] = { 255,255,255,255, 0,0,0,0, 0,0,0,0, 255,255,255,255, valueBlockB,~valueBlockB,valueBlockB,~valueBlockB }; status = mfrc522.MIFARE_Write(valueBlockB, value2Block, 16); if (status != MFRC522::STATUS_OK) { Serial.print("MIFARE_Write() failed: "); Serial.println(mfrc522.GetStatusCodeName(status)); } // Writing new value block D // Scrivo i valori per il settore C Serial.println("Writing new value block C"); byte value3Block[] = { 255,255,255,255, 0,0,0,0, 0,0,0,0, 255,255,255,255, valueBlockC,~valueBlockC,valueBlockC,~valueBlockC }; status = mfrc522.MIFARE_Write(valueBlockC, value3Block, 16); if (status != MFRC522::STATUS_OK) { Serial.print("MIFARE_Write() failed: "); Serial.println(mfrc522.GetStatusCodeName(status)); } */ Serial.println("Read block A(4) : the first of the sector TWO"); byte buffer[18]; byte size = sizeof(buffer); // change this: valueBlockA , for read anather block // cambiate valueBlockA per leggere un altro blocco status = mfrc522.MIFARE_Read(valueBlockA, buffer, &size); Serial.print("Settore : 0 Valore :"); Serial.println(buffer[0]); Serial.print("Settore : 1 Valore :"); Serial.println(buffer[1]); Serial.print("Settore : 2 Valore :"); Serial.println(buffer[2]); Serial.print("Settore : 3 Valore :"); Serial.println(buffer[3]); Serial.print("Settore : 4 Valore :"); Serial.println(buffer[4]); Serial.print("Settore : 5 Valore :"); Serial.println(buffer[5]); Serial.print("Settore : 6 Valore :"); Serial.println(buffer[6]); Serial.print("Settore : 7 Valore :"); Serial.println(buffer[7]); Serial.print("Settore : 8 Valore :"); Serial.println(buffer[8]); Serial.print("Settore : 9 Valore :"); Serial.println(buffer[9]); Serial.print("Settore :10 Valore :"); Serial.println(buffer[10]); Serial.print("Settore :11 Valore :"); Serial.println(buffer[11]); Serial.print("Settore :12 Valore :"); Serial.println(buffer[12]); Serial.print("Settore :13 Valore :"); Serial.println(buffer[13]); Serial.print("Settore :14 Valore :"); Serial.println(buffer[14]); Serial.print("Settore :15 Valore :"); Serial.println(buffer[15]) //byte value1Block[] = { 1,2,3,4, 5,6,7,8, 9,10,255,12, 13,14,15,16, valueBlockA,~valueBlockA,valueBlockA,~valueBlockA }; if ( buffer[0] == 1 && buffer[1] == 2 && buffer[2] == 3 && buffer[3] == 4 && buffer[4] == 5 && buffer[5] == 6 && buffer[6] == 7 && buffer[7] == 8 && buffer[8] == 9 && buffer[9] == 10 && buffer[10] == 255 && buffer[11] == 12 && buffer[12] == 13 && buffer[13] == 14 && buffer[14] == 15 && buffer[15] == 16 ){ // sel a scrittura è uguale alla lettura allora e stato un successo !! Serial.println("Read block A(4) : the first of the sector TWO : success"); Serial.println(":-)"); }else{ // scrittura Fallita Serial.println("Read block A(4) : the first of the sector TWO : no match - write don't work fine "); Serial.println(":-( "); } // risponde successo //Serial.println(mfrc522.GetStatusCodeName(status)); // Dump the result //mfrc522.PICC_DumpMifareClassicSectorToSerial(&(mfrc522.uid), &key, sector); // Halt PICC mfrc522.PICC_HaltA(); // Stop encryption on PCD mfrc522.PCD_StopCrypto1(); }