We can write the program in the same procedure as with ordinary Arduino. First, let’s try testing LED using following program. Set the output to pin 13 on ESP-WROOM-02.
1 2 3 4 5 6 7 8 9 10 |
void setup() { pinMode(13, OUTPUT); } void loop() { digitalWrite(13, HIGH); delay(400); digitalWrite(13, LOW); delay(400); } |
LED blinks!
So, we got the LED to blink. Let’s move onto other programs. There are several applications we can consider using its wifi communication function, for instance, to create a device that acquires data remotely, to operate like a radio control on a car, etc.
Let’s start by handling the input from the optical sensor. In Arduino UNO, there are analog input pins A0-A4, but ESP-WROOM-02 doesn’t have analog input pins like A0-A4. And there are no digital pins 1, 3-4, and 6-11. These can’t be used because the pins are used to operate the functions of the ESP-WROOM-02 by standard, such as wifi communication.
It seems like ESP-WROOM-02 does not have an analogRead function, so this can’t be used. Then, how can we make analog inputs? It looks like it’s possible to acquire analog values using the function “system_adc_read ()” on the TOUT pin.
However, this is slightly different from ordinary Arduino analog pins. The TOUT pin can detect input values from 0V to 1V, but inputting the value of 3.3V directly to the TOUT pin may damage the board. So when adding a light sensor, the power supplied from 3.3 v, it’s necessary to adjust so that the voltage entering TOUT becomes 1V maximum. We will use several resistors to create a voltage divider circuit.
ESP-WROOM-02: Program for obtaining optical sensor value
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
//necessary for analog input with ESP-WROOM-02 extern "C" { #include "user_interface.h" } void setup() { Serial.begin(115200); //also different from ordinary Arduino pinMode(13, OUTPUT); } //obtain input value from TOUT pin int getToutValue(){ int res = system_adc_read(); //get the TOUT value here return res; } void loop() { int val= getToutValue(); Serial.print("value:"); Serial.println(val); if(val > 580){ digitalWrite(13, HIGH); delay(400); } else{ digitalWrite(13, LOW); delay(400); } } |
When the program runs, the values from the optical sensor output to the serial monitor.
The analogRead function is useless in this case, but how about the analogWrite function? Arduino UNO has a pin that can also output analog pins as analog pins. How about ESP-WROOM-02? Let’s run the following program.
ESP-WROOM-02: Program for obtaining optical sensor value
1 2 3 4 5 6 7 8 9 10 |
void setup() { pinMode(13, OUTPUT); } void loop() { for(int i=0;i<256;i++){ analogWrite(13, i); delay(40); } } |
As seen in the video above, we can control the brightness of the LED. The LED gradually lights up and dims out. So it looks like we can indeed use the analogWrite function!