目的:
ESP32基板の LED(IO2) を PWM で調光する。方法:
1. LED Control PWM (LEDC) を使用して調光する。
LEDC の関数は、以下の通り。ledcSetup(チャネル番号, 周波数(Hz),dutyビット長); //チャネルへ周波数とdutyビット長を設定
ledcAttachPin(ピン番号, チャネル番号); // ピン番号へチャネルを設定
ledcWrite(チャネル番号, duty); // チャネルへdutyを設定
ledcDetachPin(ピン番号); // ピンを開放
チャネルは 0~15 の 16チャネル。
duty は 0 (0%) ~ 設定したdutyビット長の最大値(100%) を設定。
周波数は、 dutyビット長によって 最大の周波数が変わる。
試した結果では、
2. SigmaDelta 変調を使用して調光する。
SigmaDelta 変調 の関数は 以下の通り。sigmaDeltaSetup(チャネル番号, 周波数(Hz) ) ; // チャネルへ周波数を設定
sigmaDeltaAttachPin(ピン番号, チャネル番号) ; // ピン番号へチャネルを設定
sigmaDeltaWrite(チャネル番号, duty) ; // チャネルへdutyを設定
sigmaDeltaDetachPin(ピン番号) ; // ピンを開放
チャネルは 0~7 の 8チャネル。
周波数は 1220 Hz~312500 Hz の範囲。
duty は 0 (0%) ~ 255(100%) を設定。
1.も2.も同じような設定方法。チャネル数、Duty の ビット長が異なる。
出力波形は異なる。
LEDC の場合は、PWM のパルスが出力されるが、sigmaDelta変調では、対応する電圧が出力される模様。(sigmaDelta変調について、理解できていないが、、、)
例 :
LED ON ボタンを押す度に LED の明るさが変わる様に変更。(LEDC と sigmaDelta変調を併記して sigmaDelta変調をコメントアウトしている。)
/*
* WiFi LED ON/OFF TEST
* PWM Control
*/
#include <WiFi.h>
const char* ssid = "hogehoge";
const char* password = "hogehogepaswd";
IPAddress ip(192, 168, 1, 32); // for fixed IP Address
IPAddress gateway(192,168, 1, 1); //
IPAddress subnet(255, 255, 255, 0); //
IPAddress DNS(192, 168, 1, 90); //
WiFiServer server(80);
byte br_table[6] = {0,25,50,75,100,125,150,175,200,225,255} ;
int n = 0;
void setup()
{
Serial.begin(115200);
pinMode(2, OUTPUT); // set the LED pin mode
WiFi.config(ip, gateway, subnet, DNS); // Set fixed IP address
delay(10);
// We start by connecting to a WiFi network -----------------------------
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
// for LED PWM Control ---------------------------------------------------
// -- for ledc
ledcSetup(0, 5000, 13); // setup channel 0 with frequency 5000 Hz, 13 bit precission for LEDC timer
ledcAttachPin(2,0); // attach pin 2 to channel 0
ledcWrite(0, 0); // initialize channel 0 to off
// -- for sigmaDelta
//sigmaDeltaSetup(0, 312500); // setup channel 0 with frequency 312500 Hz
//sigmaDeltaAttachPin(2,0); // attach pin 2 to channel 0
//sigmaDeltaWrite(0, 0); // initialize channel 0 to off
n=0 ;
}
void loop(){
WiFiClient client = server.available(); // listen for incoming clients
if (client) { // if you get a client,
Serial.println("***** Client access start *****"); // print a message out the serial port
while (client.connected()) { // loop while the client's connected
if (client.available()) { // if there's bytes to read from the client,
String line = client.readStringUntil('\n'); // Get Line data until '\n'
Serial.println(line);
if (line.indexOf("GET /?on") != -1) { // Client request was "GET /?on"
n += 1 ; // incliment table no.
if (n>5) n=0 ;
Serial.print("brightness value : ");
Serial.println(br_table[n],DEC);
// -- for ledc
ledcWrite(0, 8191*br_table[n]/255) ; // set PWM value to channel#0
// -- for sigmaDelta
//sigmaDeltaWrite(0, br_table[n]) ;
}
if (line.indexOf("GET /?off") != -1) { // Client request was "GET /?off"
n = 0 ; // clear bright no.
Serial.print("brightness value : ");
Serial.println(br_table[n],DEC);
// -- for ledc
ledcWrite(0, 8191*br_table[n]/255) ; // set PWM value to channel#0
// -- for sigmaDelta
//sigmaDeltaWrite(0, br_table[n]) ;
}
if (line.length() == 1 && line[0] == '\r'){ // end of HTTP request
send_response(client) ; // send response to client
break; // break while loop
}
}
}
delay(1); // give the web browser time to receive the data
// close the connection:
client.stop();
Serial.println("Client Disconnected.");
Serial.println("--------------------------------------------------");
}
}
// ------------------------------------------------------------------
void send_response(WiFiClient client) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// the content of the HTTP response follows the header:
client.print("<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>" ) ;
client.print("<style>input {margin:8px;width:100px;}div {font-size:16pt;text-align:center;width:250px;border:solid 4px #93ff93;}</style>" ) ;
client.print("<title>Color LED Controller</title></head>" ) ;
client.print("<body><div><p>LED ON/OFF</p><form method='get'>" ) ;
client.print("<input type='submit' name='on' value='ON' /><input type='submit' name='off' value='OFF' /></form>" ) ;
client.print("</div></body></html>" ) ;
client.println();
Serial.println( " --- send responce --- ");
}
2018/12/8 更新 : LEDCを追記。出力波形について追記。
2019/1/14 更新 : LEDCの最大の周波数について追記。
0 件のコメント:
コメントを投稿