Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By Sunspot
#21092 esp01 would be fantastic with a good i2c driver - so may chips out there for i2c!

Some people have put up I2C examples in the past but none work for me on the latest IDE
but I can run the scanner and see MCP23017 and PCF8574 addresses :-
// --------------------------------------
// i2c_scanner
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26 2013
// V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
// by Arduino.cc user Krodal.
// Changes by louarnold removed.
// Scanning addresses changed from 0...127 to 1...119,
// according to the i2c scanner by Nick Gammon
// http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
// As version 4, but address scans now to 127.
// A sensor seems to use address 120.
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.

#include <Wire.h>

void setup()
Wire.begin(0, 2);

Serial.println("\nI2C Scanner");

void loop()
byte error, address;
int nDevices;


nDevices = 0;
for(address = 1; address < 127; address++ )
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
error = Wire.endTransmission();

if (error == 0)
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.println(" !");

else if (error==4)
Serial.print("Unknow error at address 0x");
if (address<16)
if (nDevices == 0)
Serial.println("No I2C devices found\n");

delay(1000); // wait 5 seconds for next scan
Note that it just needs Wire.h and not i2c.h in addition etc as other earlier examples
note also the Wire.begin(0, 2);

Adafruit MCP23017 example gives nothing on the i2c lines with a 'scope
knightrider gives pulses but fails to control the pcf8574
Wire.setClock(any number); - does nothing - I seem to get 100k in knightrider all the time

Can anyone share i2c examples that actually work?

Please . . .
User avatar
By Sunspot
#21107 I use your link with Arduino 1.6.5

I see i2c section -

"I2C (Wire library)
Wire library currently supports master mode up to approximately 450KHz. Before using I2C, pins for SDA and SCL need to be set by calling Wire.begin(int sda, int scl), i.e. Wire.begin(0, 2); on ESP-01, else they default to pins 4(SDA) and 5(SCL)."

and I include

Wire.begin(0, 2);

- it works fine on the scanner

I just can't get any other Arduino i2c code to run.
Adafruit MCP23017 compiles OK but all lines stay dead on the scope
knight rider at least puts out pulses but no change in PCF8574 IO pins

There was a "Sketch Buffet" that I hoped to copy but it would not compile with the current SDE
That include i2c.h but the current IDE did not like that
and why does Wire.setClock() compile OK but do nothing?

The scanner just uses Wire.h alone and detects both port expanders fine

Someone must be running i2c OK? - it is so useful!
User avatar
By martinayotte
#21111 When you say that
Adafruit MCP23017 compiles OK but all lines stay dead on the scope
Is your pins are on 0,2 or (4, 5) ? because as I said on another thread :

BTW, maybe it can help : several weeks ago, I posted that using alternate pins didn't work for me until I added the following code in the begin() function :
Code: Select all

void TwoWire::begin(int sda, int scl){
+ default_sda_pin = sda;
+ default_scl_pin = scl;
twi_init(sda, scl);

I didn't investigate further, but maybe the members are still keeping its previous values.

About my "Sketch Buffet", if your look on this post viewtopic.php?f=29&t=2319#p20099, you should be able to compile it.
But when I got chance, I will clean it up to avoid this i2c.h dependency.

EDIT : I've look right away about "ic2.h" dependency issue, but with my current tree, it doesn't seems to be needed anymore. Can you show the error you are getting ?