Skip to main content

ESP-12E I2C test communication with ATtiny85

I ran a little test to make sure the ESP-12E is able to communicate with the ATtiny85 via I2C.

These are the connections required:


D1 and D2 are both pulled up to 3V3 on the ESP-12E with 10K resistors. The ESP-12E and the ATtiny85 are connected via different USB cables (ATtiny85 via the Arduino Uno) to the same USB hub.

The sketch for the ESP-12E (I2C master):

 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
31
32
33
34
#include <stdint.h>
#include <Wire.h>

byte tx = 0;

void debug(const char *format, ...) {
  char buf[256];
  va_list ap;
  va_start(ap, format);
  vsnprintf(buf, sizeof(buf), format, ap);
  va_end(ap);
  Serial.println(buf);
}

void setup() {
  Serial.begin(115200);
  Wire.begin();
  Wire.setClockStretchLimit(1500); 
}

void loop() {
  Wire.beginTransmission(0x26);
  Wire.write(++tx);
  Wire.endTransmission();
  debug("tx = %d", tx);
  
  Wire.requestFrom(0x26, 1);
  if (Wire.available() >= 1) {
    byte rx = Wire.read();
    debug("rx = %d", (int)rx);
  }  

  delay(1000);
}

The sketch for the ATtiny85 (I2C slave):

 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include "TinyWireS.h"

#define I2C_SLAVE_ADDR  0x26 // i2c slave address (38)

bool timer1 = false, led = true;
byte numInterrupts = 0, recvByte = 0;

// Interrupt service routine for timer1
ISR(TIMER1_COMPA_vect)
{
  timer1 = true;
}

void requestEvent() {  
  TinyWireS.send(recvByte + 11);
}

void receiveEvent(uint8_t howMany) {
  if (TinyWireS.available() && howMany == 1) recvByte = TinyWireS.receive();
}

void setup() {

  // Misc setup
  pinMode(3, OUTPUT);
  set_sleep_mode(SLEEP_MODE_IDLE);

  // Setup timer1 to interrupt every second
  TCCR1 = 0; // Stop timer
  TCNT1 = 0; // Zero timer
  GTCCR = _BV(PSR1); // Reset prescaler
  OCR1A = 243; // T = prescaler / 1MHz = 0.004096s; OCR1A = (1s/T) - 1 = 243
  OCR1C = 243; // Set to same value to reset timer1 to 0 after a compare match
  TIMSK = _BV(OCIE1A); // Interrupt on compare match with OCR1A
  
  // Start timer in CTC mode; prescaler = 4096; 
  TCCR1 = _BV(CTC1) | _BV(CS13) | _BV(CS12) | _BV(CS10);
  sei();

  TinyWireS.begin(I2C_SLAVE_ADDR);
  TinyWireS.onReceive(receiveEvent);
  TinyWireS.onRequest(requestEvent);
}

void loop() {
  // This is required by TinyWire
  TinyWireS_stop_check();

  // Blink LED
  if (timer1) {
    timer1 = false;
    digitalWrite(3, led ? HIGH : LOW);
    led = !led;
  }
  
  // Sleep to save power
  sleep_enable();
  sleep_cpu();
}

The expected output from the ESP-12E is:

 tx = 1
 rx = 12
 tx = 2
 tx = 13
 tx = 3
 rx = 14

and so on, all with the LED blinking on the ATtiny85 at roughly 1Hz.

There are very few sources I could find on the Internet on interfacing ESP-12E (or ESP8266) with ATtiny via I2C, so let this post be a confirmation that it can be done quite easily.

I took note of Liebman's comment below and added this line:

  Wire.setClockStretchLimit(1500); 

Works a treat, and I was able to run ATtiny85 at 1MHz without impacting the reliability of the I2C communication with ESP-12E.

The other thing I realized was that running the ATtiny85 at 1MHz is unreliable for I2C communication. After an indeterminate amount of time, it will get stuck. The author himself says that it has never been tested for 1MHz. So to get it working reliably, I had to run the ATtiny85 at 8MHz. This can be done by setting the internal clock to 8MHz from the "Tools" menu:



then burning the correct fuses by selecting "Burn Bootloader":



After I did that, the communication has been verified to go on reliably for hours without any hiccup.

However, because of this increase in clock speed, I had to make minor changes to the Timer1 interrupt logic. At 8Mhz, the slowest interrupt rate that can be achieved is 0.5Hz (twice a second). As such, I had to add a counter to the ISR to set timer1 to true every second time the ISR is called.

Notes:

  • Changed LED output PIN from PB1 to PB3. Since PB1 is involved in the programming of ATtiny85, it is possible to encounter intermittent failure with the upload of programs when the LED is blinking. Changing the output PIN to PB3 (which is not connected to the programmer) eliminates this issue.
  • Since PB0 and PB2 are used for programming the ATtiny85 as well as I2C communication with the ESP-12E, these connections have to be removed from the ATtiny85 before uploading a program.
► Breadboard diagram created using Fritzing.

Comments

  1. I've found that setting the clock pre-scaler on the ATtiny85 to to 2, reducing the clock from 8mhz to 4mhz in setup() will reduce power consumption by ~ 50% for non sleep modes. I2C with the ESP still seemed to function, I also attempted 1mhz but I also found that I2C fails on the ATtiny85. The only side effect is that any standard Arduino timing functions will not be correct, which does not matter for my implementation as I don't use them, I'm time keeping with a DS3231 RTC.

    ReplyDelete
  2. ATtiny85 works fine at 1mhz. The issue us that the ESP as a master has a fairly short limit on clock stretch support. This is configurable and I've had good luck, so far, using 1500us as opposed to the default 230us. I've tested with USIWire as well as the "wire" library supplied with ATTinyCore.

    ReplyDelete
  3. where is the TinyWireS.h can download? I had try many TinyWireS.h that all cant use the "TinyWireS.onReceive(receiveEvent);" ....
    thanks

    ReplyDelete
  4. Is possible using I2C and UART on the same time ?

    ReplyDelete

Post a Comment

Popular posts from this blog

Adding "Stereo Mixer" to Windows 7 with Conexant sound card

This procedure worked for my laptop (Thinkpad E530) with a Conexant 20671 sound card, but I suspect it will work for other sound cards in the Conexant family. I was playing with CamStudio to do a video capture of a Flash-based cartoon so that I can put it on the WDTV media player and play it on the big screen in the living room for my kids. The video capture worked brilliantly, but to do a sound capture, I needed to do some hacking. Apparently, there was this recording device called "Stereo Mixer" that was pretty standard in the Windows XP days. This allowed you to capture whatever was played to the speaker in all its digital glory. Then under pressure from various organizations on the dark side of the force, Microsoft and soundcard makers starting disabling this wonderful feature from Windows Vista onwards. So after much Googling around, I found out that for most sound cards, the hardware feature is still there, just not enabled on the software side. Unfortunately, to

Attiny85 timer programming using Timer1

This Arduino sketch uses Timer1 to drive the LED blinker: 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 /* * Program ATTiny85 to blink LED connected to PB1 at 1s interval. * Assumes ATTiny85 is running at 1MHz internal clock speed. */ #include <avr/io.h> #include <avr/wdt.h> #include <avr/sleep.h> #include <avr/interrupt.h> bool timer1 = false , led = true ; // Interrupt service routine for timer1 ISR(TIMER1_COMPA_vect) { timer1 = true ; } void setup() { // Setup output pins pinMode( 1 , OUTPUT); digitalWrite( 1 , led); set_sleep_mode(SLEEP_MODE_IDLE); // Setup timer1 to interrupt every second TCCR1 = 0 ; // Stop timer TCNT1 = 0 ; // Zero timer GTCCR = _BV(PSR1); // Reset prescaler OCR1A = 243 ; // T = prescaler / 1MHz = 0.004096s; OCR1A = (1s/T) - 1 = 243 OCR1C = 243 ; // Set to same value to reset timer1 to

Hacking an analog clock to sync with NTP - Part 5

This is how it looks after I have put everything together. The Arduino sketch is available here . The 2 jumper wires soldered to the clock mechanism are connected to pins D0 and D1 on the ESP-12 (in any order). When the device first boots up, it presents an access point which can be connected to via the PC or smartphone. Once connected, the captive portal redirects the web browser to the configuration page:     A custom field has been added to the WiFi configuration page to enter the current clock time in HHMMSS format. Try to set the clock time to as close to the current time as possible using the radial dial at the back of the clock so the clock will have less work to do catching up. In the config page, the HTML5 Geolocation API is also used to obtain your current location (so if your web browser asks if you would like to share your location, answer "yes"). This is then passed to the Google Time Zone API to obtain the time and DST offset of your time z