ความเห็น: 0
การใช้งานขา GPIO บน ESP8266
การใช้งานขา GPIO บน ESP8266
- ติดตั้ง Arduino IDE กับ ESP8266 พร้อม flash โปรแกรม
- การใช้งานขา GPIO บน ESP8266
ก่อนอื่น มาคลายความสงสัยกันก่อน ว่า GPIO คืออะไร ? … GPIO ย่อมาจาก General Purpose Input/Output เรียกเป็นภาษาไทยง่ายๆว่า พอร์ตเอนกประสงค์ ที่เป็นได้ทั้ง อินพุต และ เอาต์พุต คือเราสามารถควบคุม คอนโทรลให้เป็นลอจิก “1” หรือ ลอจิก “0” ได้ ตามใจชอบ โดยที่จะสามารถควบคุมได้แต่ละ pin เหมือนไมโครคอนโทรลเลอร์ทั่วไป
1. มารู้จักโมดูล ESP8266 ที่มีขายและ ใช้งานทั่วไปๆ กันก่อนเลยครับ
โมดูลที่นิยมใช้งานทั่วไป ๆ ของ ESP8266 มีอยู่ 2 รุ่น ได้แก่ ESP8266-01 และ ESP8266-12 ทั้งตัวมีเสา กับไม่มีเสา ซึ่งเป็นสิ้งค้าของบริษัท AI-THINKER
ESP8266-01
โมดูล ESP8266-01 รุ่นนี้ มีขาใช้งาน 8 ขา ใช้แรงดันไฟฟ้ากระแสตรง 3.3 โวลต์ มีขา GPIO 2 ขา และขาการสื่อสารแบบ UART คือ TX และ RX
ESP8266-12
โมดูล ESP8266-12 รุ่นนี้ มีขาใช้งาน 16 ขา ใช้แรงดันไฟฟ้ากระแสตรง 3.3 โวลต์ มีขา GPIO 9 ขา, มีขา ADC 1 ขา, มีขาการสื่อสารแบบ UART , ในขา GPIO จะมีขาใช้งานในการสื่อสารอื่นแฝงอยู่ทั้งแบบ I2C และ SPI
จะเห็นได้ว่าภายในโมดูล ESP8266 จะประกอบไปด้วย ชิพไมโครคอนโทรลเลอร์ และ ชิพที่ใช้สำหรับการเชื่อมต่อ WIFI นั่นหมายถึงเราจะสามารถใช้งาน ไมโครคอนโทรลเลอร์ที่สามารถต่อ WIFI ได้นั่นเอง
2. มารู้จัก break out board สำหรับ โมดูล ESP8266 กันต่อเลยครับ
break out board สำหรับ โมดูล ESP8266 ที่นิยมใช้งานทั่วไป ๆ มีอยู่หลายรุ่นเลยทีเดียว เช่น บอร์ด DW.mini.ESP8266 ของ Deaware, บอร์ด NodeMCU ของ Seeedstudio เป็นต้น
บอร์ด DW.mini.ESP8266 ของ Deaware
ตารางขาใช้งาน GPIO ของโมดูล ESP8266-12
บอร์ด NodeMCU Development ของ Seeedstudio
บอร์ด NodeMCU V1 ของ Seeedstudio
ซึ่ง break out board สำหรับ โมดูล ESP8266 ในแต่ละบริษัทที่ผลิตออกมานั้นล้วนแต่มีวัตถุประสงค์เพื่อ อำนวยความสะดวกสะบายในการต่อใช้งาน ให้กับผู้ใช้งาน ทั้งสิ้น ในบางรุ่นอาจจะมี ไม่โครคอนโทรลเลอร์เพิ่มเข้ามาซึ่งจะช่วยในการประมวลผล หรือช่วยในการสื่อสาร นั่นเอง
สำหรับ การเริ่มต้นใช้งาน ESP8266 ผ่าน Arduino IDE สามารถตามอ่านได้จากตอนที่ 1 >> Click
ในการทดสอบ ผมขออณุญาติใช้ บอร์ด DW.mini.ESP8266 ของ Deaware ในการ Demo บทความในครั้งนี้ครับ ในบอร์ดรุ่น อื่น ๆ นั้น สามารถหาอ่านข้อมูลเพิ่มเติมได้ที่แหล่งข้อมูลดีดี ข้างล่างบทความได้เลย
3. การใช้งาน Digital I/O ของ ESP8266 สามารถใช้งานคำสั่ง digitalWrite, digitalRead ได้เช่นเดียวกับการใช้งาน Digital I/O ของ Arduino โดยกำหนดหมายเลขของ Pin ที่ต้องการใช้งาน
ตัวอย่างการต่อวงจร Digital Output
ตัวอย่างโปรแกรม Digital Output
/*Blink*/ | |
void setup() { | |
// initialize digital pin 13 as an output. | |
pinMode(1, OUTPUT); | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
digitalWrite(1, HIGH); // turn the LED on (HIGH is the voltage level) | |
delay(1000); // wait for a second | |
digitalWrite(1, LOW); // turn the LED off by making the voltage LOW | |
delay(1000); // wait for a second | |
} |
จาก Code ตัวอย่าง จะใช้ขา GPIO1 ซึ่งเป็นขาที่ใช้สื่อสารกับ Serial Port โดยตรง แบบ UART จะมี LED สีน้ำเงิน ต่ออยู่เพื่อแสดงสถานะ การส่งข้อมูล (หากใช้งานขา GPIO1 จะไม่สามารถใช้งาน Serial Port ได้) การทำงานของ โปรแกรมคือ LED บนบอร์ด DW.mini.ESP8266 จะกระพริบ ทุกๆ 1 วินาที เหมือนกับตัวอย่าง Example : Blink บน Arduino IDE
ตัวอย่างการต่อวงจร Digital Input
ตัวอย่างโปรแกรม Digital Input
/*Button*/ | |
// set pin numbers: | |
const int buttonPin = 16; // the number of the pushbutton pin | |
const int ledPin = 1; // the number of the LED pin | |
// variables will change: | |
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); | |
// 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); | |
} | |
} |
จาก โปรแกรมตัวอย่าง Digital Output จะเพิ่ม ปุ่ม (botton) เข้ามาเพื่อรับค่า Digital Input ที่มี ลอจิก “0” กับ ลอจิก “1” โดยมีตัวต้านทานต่อแบบ Pull Down อยู่ จะทำให้ สวิทต์ มีสภาวะการทำงานแบบ Active HIGH การทำงานของโปรแกรม หากมีการกด ปุ่ม (botton) ไฟ LED บนบอร์ด จะติด และหากปล่อย ปุ่ม (botton) ไฟ LED บนบอร์ด ก็จะดับ
วีดีโอสาธิต การทดลอง Digital I/O ของ ESP8266
4. การใช้งาน Analog I/O (ADC) และ (PWM) ของ ESP8266 สามารถใช้งานคำสั่ง analogWrite, analogRead ได้เช่นเดียวกับการใช้งาน Analog I/O ของ Arduino เลยครับ
ตัวอย่างการต่อวงจร Analog Input (ADC)
*** ขา ADC ของ ESP8266 สามารถรับแรงดันได้สูงสุด 1V ความละเอียด 10 บิต ที่ 1023
ตัวอย่างโปรแกรม Analog Input (ADC)
/* | |
Analog input, analog output, serial output | |
*/ | |
// These constants won't change. They're used to give names | |
// to the pins used: | |
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to | |
int sensorValue = 0; // value read from the pot | |
void setup() { | |
// initialize serial communications at 9600 bps: | |
Serial.begin(115200); | |
} | |
void loop() { | |
// read the analog in value: | |
sensorValue = analogRead(analogInPin); | |
// print the results to the serial monitor: | |
Serial.print("sensor = " ); | |
Serial.println(sensorValue); | |
delay(50); | |
} |
จาก โปรแกรมตัวอย่าง Analog Input (ADC) เป็นการอ่านค่า ระดับแรงดันที่ตกคร่อม ตัวต้านทานเปลี่ยนค่าตามแสง (LDR) โดยต่อแบบ แบ่งแรงดันที่แหล่งจ่าย 3.3V ใช้ตัวต้านทาน (R 220กิโลโอห์ม) เป็นตัวแบ่งแรงดันไม่ให้แรงดันมาตกคร่อม (LDR) มากจนเกินไป ซึ่ง LDR มีค่าความต้านทานสูงสุด ที่ 100 กิโลโอห์ม จะมีแรงดันสูงสุดตกคร่อม (LDR) ประมาณ 1V และต่อขาสัญญาณไปเข้าขา ADC ของ ESP8266 ซึ่งขา ADC มีขื่อคือขา A0 และปริ้นค่าออกทาง Serial Port.
ตัวอย่างการต่อวงจร Analog Output (PWM) ความถี่ปกติ 1KHz
ตัวอย่างโปรแกรม Analog Output (PWM) ความถี่ปกติ 1KHz
/* | |
RGB Fading | |
*/ | |
#include <EEPROM.h> | |
const int maxdelay = 99; | |
const int maxmode = 4; | |
const int redpin = 16; // LED connected to digital pin 11 | |
const int bluepin = 14; // LED connected to digital pin 10 | |
const int greenpin = 12; // LED connected to digital pin 9 | |
int oldr = 1023; | |
int oldg = 1023; | |
int oldb = 1023; | |
int mode = 1; | |
int delayval = 1; | |
char* modes[]={"","Fade smoothly with variable delay","Pulse in and out","Fade in steps--broken","Blink"}; | |
void setup() { | |
// nothing happens in setup | |
analogWrite(redpin,oldr); | |
analogWrite(bluepin,oldb); | |
analogWrite(greenpin,oldg); | |
randomSeed(analogRead(0)); | |
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps | |
delayval = constrain(EEPROM.read(0),1,maxdelay); | |
mode = constrain(EEPROM.read(1),1,maxmode); | |
Serial.println("Reading settings from EEPROM..."); | |
Serial.print("Mode is "); | |
Serial.println(mode); | |
Serial.print("Delay is "); | |
Serial.println(delayval); | |
} | |
void loop() { | |
if ( Serial.available() > 2) { // there must be 3 or more characters in the buffer | |
char cmd = Serial.read(); | |
char arg1 = Serial.read(); | |
char arg2 = Serial.read(); | |
int arg = ((arg1-'0')*10)+(arg2-'0'); // convert the args from ascii and stick them together | |
Serial.flush(); | |
Serial.print("Received cmd:"); | |
Serial.print(cmd); | |
Serial.println(arg); | |
switch (cmd) { | |
case 'm': | |
mode=constrain(arg,1,maxmode); | |
Serial.print("Setting mode to "); | |
Serial.print(mode); | |
Serial.print(" ("); | |
Serial.print(modes[mode]); | |
Serial.println(")"); | |
EEPROM.write(1,mode); | |
break; | |
case 'd': | |
delayval=constrain(arg,1,99); | |
Serial.print("Setting delayval to "); | |
Serial.println(delayval); | |
EEPROM.write(0,delayval); | |
break; | |
default: | |
Serial.println("Invalid command, doing nothing."); | |
Serial.print("Mode is still '"); | |
Serial.print(mode); | |
Serial.print("', delay is still '"); | |
Serial.print(delayval); | |
Serial.println("'"); | |
break; | |
} | |
} | |
switch (mode) { | |
case 1: | |
random_fade3(delayval); | |
break; | |
case 2: | |
random_fade1(); | |
break; | |
case 3: | |
random_fade2(); | |
break; | |
default: | |
random_blink(); | |
} | |
} | |
void random_fade1() { | |
int newr = random(1,1023); | |
int newg = random(1,1023); | |
int newb = random(1,1023); | |
int scale = 51; | |
for (int i = 0; i <= scale; i +=1) { | |
analogWrite(redpin, 1023-(i*(newr/scale))); | |
analogWrite(greenpin, 1023-(i*(newg/scale))); | |
analogWrite(bluepin, 1023-(i*(newb/scale))); | |
delay(delayval); | |
} | |
for (int i = scale; i > 0; i -=1) { | |
analogWrite(redpin, 1023-(i*(newr/scale))); | |
analogWrite(greenpin, 1023-(i*(newg/scale))); | |
analogWrite(bluepin, 1023-(i*(newb/scale))); | |
delay(delayval); | |
} | |
} | |
void random_fade2() { | |
int newr = random(1,1023); | |
int newg = random(1,1023); | |
int newb = random(1,1023); | |
int scale = 1023; | |
float rscale=(oldr-newr)/scale; | |
float gscale=(oldg-newg)/scale; | |
float bscale=(oldb-newb)/scale; | |
for (int i = 0; i <= scale; i +=1) { | |
analogWrite(redpin, oldr-int(float(i)*rscale)); | |
analogWrite(greenpin, oldg-int(float(i)*gscale)); | |
analogWrite(bluepin, oldb-int(float(i)*bscale)); | |
delay(delayval); | |
} | |
oldb = newb; | |
oldr = newr; | |
oldg = newg; | |
} | |
void random_fade3(int delayval) { | |
int newr = random(1,1023); | |
int newg = random(1,1023); | |
int newb = random(1,1023); | |
fade_one(dela
สร้าง: 18 พฤศจิกายน 2559 22:46
แก้ไข: 18 พฤศจิกายน 2559 22:46
[ แจ้งไม่เหมาะสม ]
บันทึกอื่นๆ
|
ร่วมแสดงความเห็นในหน้านี้