Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By Erik84750
#76622 I simple sketch where I read GPIO16 to set all outputs GPIO0, 2, 12, 13, 47 to either HIGH or LOW.
All outputs work fine except GPIO0.
There is just a 10k pullup connected to it, but even if I remove that and rely on the internal pullup the output does not go low to a stable low voltage, instead I get oscillations: see picture attached.

I tried this with several SEP8266-12F, all show the same behavior. The load is the same everywhere: a 50R connected to a N-Channel MOSFET that is used to drive the load (LED's).

[code]
/* Testing of ESP8266
*
* One digital input commands all digital outputs
* LED state change per button press
*
*/

#define DS3231_I2C_ADDRESS 0x68
#include <Wire.h>
const int buttonPin1 = 16;
// const int buttonPin2 = 13;
const int LED1 = 14;
const int LED2 = 12;
const int LED3 = 13;
const int LED4 = 12;
const int LED5 = 02;
const int LED6 = 00;



int ledState = HIGH;
int buttonState1;
int buttonState2;
int lastButtonState1 = LOW;
int lastButtonState2 = LOW;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
unsigned long elapsedTime;
unsigned long startTime;
unsigned long currentTime;

// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val / 10 * 16) + (val % 10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val / 16 * 10) + (val % 16) );
}

void setup() {

pinMode(buttonPin1, INPUT);
// pinMode(buttonPin2, INPUT);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
pinMode(LED5, OUTPUT);
pinMode(LED6, OUTPUT);

Serial.begin(115200); // Setup Serial Communication.
digitalWrite(LED1, ledState);
}


/**************************
R E A D T I M E
**************************/

void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}


void actionTime() {
// do something once, using Ds3231: read time and
// act only if certain time criteria are met
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
// retrieve data from DS3231
readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);

static byte lastSecond = 0;
if ( second == lastSecond ) {
return;
}
lastSecond = second; // remember what minute was last time we looked
// minute has changed... what is it now?
if (second == 01)
{
// do something once
}

}

void loop() {

currentTime = millis();
// debounce
if (currentTime - lastDebounceTime > debounceDelay) {
lastDebounceTime += debounceDelay;
buttonState1 = digitalRead(buttonPin1);

// then check the change of state
if (buttonState1 != lastButtonState1) {
if (buttonState1 == HIGH) {
ledState = !ledState;
if (ledState == HIGH) {
startTime = currentTime;
}
}
lastButtonState1 = buttonState1;
}
}
digitalWrite(LED1, ledState);
digitalWrite(LED2, ledState);
digitalWrite(LED3, ledState);
digitalWrite(LED4, ledState);
digitalWrite(LED5, ledState);
digitalWrite(LED6, ledState);

/*
// manage LED2
if (currentTime - startTime > 3000 && ledState == HIGH) {
digitalWrite(LED2, HIGH);
}
else {
digitalWrite(LED2, LOW);
}
*/

actionTime(); // do something, time based
}

[code]
You do not have the required permissions to view the files attached to this post.
User avatar
By Mr.Gil
#88733 First check your power supply and be sure it is stable. Note not all is as it appears; several of the GPIO pins are used for other functions. GPIO0 along with GPIO2 and GPIO15 is read during boot and controls how the ESP8266 will start. Try this link, it gives a good explanation of what is happening. https://www.forward.com.au/pfod/ESP8266 ... index.html
I cannot stress to much on how many problems can be traced back to the power supply. I am on the lazy side so I use I/O expanders such as the PCF8574 and many others. This also makes interfacing to 5 volt logic a piece of cake. None of the pins are 5V tolerant but the design of many I2C devices use open drain to drive the buss and can be at 5V without any problems.
User avatar
By Erik84750
#88737
Mr.Gil wrote:First check your power supply and be sure it is stable. Note not all is as it appears; several of the GPIO pins are used for other functions. GPIO0 along with GPIO2 and GPIO15 is read during boot and controls how the ESP8266 will start. Try this link, it gives a good explanation of what is happening. https://www.forward.com.au/pfod/ESP8266 ... index.html
I cannot stress to much on how many problems can be traced back to the power supply. I am on the lazy side so I use I/O expanders such as the PCF8574 and many others. This also makes interfacing to 5 volt logic a piece of cake. None of the pins are 5V tolerant but the design of many I2C devices use open drain to drive the buss and can be at 5V without any problems.


Thank you very much for this information! I had solved this issue back shortly after posting this in 2018, unfortunately I cannot remember how; in any case, I can since then use GPIO0 as an output.

Using the PCF8574 (8 bit), or for me the MCP23017 (16 bit) as a I/O expander, is I think a good idea.

The link you supplied already is in my favourite list: good information!

At start-up GPIO0 is used as an input by the ESP8266 to determine the status: either to be programmed, or as a controller. After that I use it as an output pin.