ESP32 reading from Attiny85 sensor through I2C protocol

tpires
Posts: 3
Joined: Mon Oct 05, 2020 2:13 pm

ESP32 reading from Attiny85 sensor through I2C protocol

Postby tpires » Mon Oct 05, 2020 3:02 pm

Hi all,
I'm having difficulties connecting a ESP32 to an attiny85 through i2C protocol.
The attiny was programmed with an Arduino UNO (in arduino IDE) and is programmed to read a current and send two bytes with the reading upon request as follows:

Code: Select all

//UPLOAD TO ATTINY85 in order to pass on readings from tuner without oscilations

#include <TinyWireS.h>

#ifndef TWI_RX_BUFFER_SIZE
#define TWI_RX_BUFFER_SIZE ( 16 )
#endif

#define I2C_SLAVE_ADDRESS 0x20
#define ADC_PIN A3

void readtoi2c () {
  
  int i;
  volatile uint8_t i2c_regs[] = {0,0};
  volatile unsigned int PotMeter;
  const byte reg_size = sizeof(i2c_regs);
  
  PotMeter = analogRead(ADC_PIN);
  i2c_regs[0] = PotMeter >> 8;
  i2c_regs[1] = PotMeter & 0xFF;

  TinyWireS.send(i2c_regs[0]);
  TinyWireS.send(i2c_regs[1]);

}

// the setup routine runs once when you press reset:
void setup() {
  
  //Setup I2C
  TinyWireS.begin(I2C_SLAVE_ADDRESS);
  TinyWireS.onRequest(readtoi2c);

  //Setup pins
  pinMode(ADC_PIN, INPUT);

}

// the loop routine runs over and over again forever:
void loop() {

}
I'm trying to read the current value with the ESP32 with the following code (ESP32 programmed through ESP IDF):

Code: Select all


//Include Libraries
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "nvs.h"
#include "nvs_flash.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_err.h"
#include "driver/i2c.h"
#include "driver/gpio.h"
#include "driver/adc.h"
#include "sdkconfig.h"
#include "math.h"

// Constants
#define ATTINY85_ADDR				0x20

void attiny_get_freq () {	
	uint8_t buffer[2] = {0,0};

	float freqSet;
	int i;
	
	i2c_config_t conf;
	conf.mode = I2C_MODE_MASTER;
	conf.sda_io_num = 18;
	conf.scl_io_num = 19;
	conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
	conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
	conf.master.clk_speed = 100000;
	ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
	
	i2c_cmd_handle_t cmd = i2c_cmd_link_create();
	i2c_master_start(cmd);
	i2c_master_write_byte(cmd, (ATTINY85_ADDR << 1) | I2C_MASTER_READ, 1);
	i2c_master_read_byte(cmd, &buffer[0], 0x0);
	i2c_master_read_byte(cmd, &buffer[1], 0x1);
	i2c_master_stop(cmd);
	i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000 / portTICK_RATE_MS);
	i2c_cmd_link_delete(cmd);
	
	freqSet = (((buffer[0]&0x3F)<<8)+buffer[1]);
	
	printf ("Current = %f MHz\n\n",freqSet);
	
	//vTaskDelay(pdMS_TO_TICKS(1000));
}


void app_main()
{
	
	// Install the I2C driver
	ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
	
	while (1) {
		// Run function to read attiny
		attiny_get_freq ();
		vTaskDelay(pdMS_TO_TICKS(100));
	}
}
I cannot get the ESP32 to communicate with the attiny85.

The wiring diagram is attached.

Can you please help? What am I doing wrong?

Thanks in advance and best regards,
Tiago
Attachments
Schematics_ESP32_Attiny85.png
Schematics for the circuit
Schematics_ESP32_Attiny85.png (152.8 KiB) Viewed 4773 times

ESP_Sprite
Posts: 9040
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32 reading from Attiny85 sensor through I2C protocol

Postby ESP_Sprite » Tue Oct 06, 2020 7:05 am

First of all, I2C needs pullup resistors to Vcc to work. Suggest you add two resistors (anywhere between say 2.2K to 10K) to the SCL and SDA lines to 3.3V.

If that doesn't work: Can you expand a bit on what results you *do* get? Do you have a logic analyzer or scope to look at the signals?

boarchuz
Posts: 566
Joined: Tue Aug 21, 2018 5:28 am

Re: ESP32 reading from Attiny85 sensor through I2C protocol

Postby boarchuz » Tue Oct 06, 2020 12:57 pm

Also the Tiny at 5V could well be a problem: a logic high on the I2C bus may not be high enough.
Investigate powering it from 3v3 or using level shifters.

tpires
Posts: 3
Joined: Mon Oct 05, 2020 2:13 pm

Re: ESP32 reading from Attiny85 sensor through I2C protocol

Postby tpires » Tue Oct 06, 2020 6:14 pm

ESP_Sprite wrote:
Tue Oct 06, 2020 7:05 am
First of all, I2C needs pullup resistors to Vcc to work. Suggest you add two resistors (anywhere between say 2.2K to 10K) to the SCL and SDA lines to 3.3V.

If that doesn't work: Can you expand a bit on what results you *do* get? Do you have a logic analyzer or scope to look at the signals?
I've tried pulling the SLC and SDA lines to 3.3V with 3k resistor on each line without success, I've also connected the tiny to 3.3V with the same result. I'm sorry but i do not have a scope to further troubleshoot the issue...

I've tried a check on the tiny with function i2c_master_cmd_begin() and it returns "ESP_ERR_TIMEOUT".
Can this be an incompatibility between the tiny being programmed by the Arduino Uno (through arduino IDE) and now being integrated with the ESP32 (through ESP IDF)? Or an incompatibility between my programming of the ESP 32 and the wire/tinywireS.h libraries used to program the tiny? I'm not really knowledgeable with the I2C protocol so there can be some specifications that I missed in the ESP32 programming that are not compatible with the tiny?
Considering that I'm not using wire library in ESP IDF, is my approach the correct to trigger the reading on the tiny that is waiting for a command equivalent to "Wire.requestFrom()"?
Again, thank you so much for the help!

tpires
Posts: 3
Joined: Mon Oct 05, 2020 2:13 pm

Re: ESP32 reading from Attiny85 sensor through I2C protocol

Postby tpires » Tue Oct 06, 2020 6:18 pm

boarchuz wrote:
Tue Oct 06, 2020 12:57 pm
Also the Tiny at 5V could well be a problem: a logic high on the I2C bus may not be high enough.
Investigate powering it from 3v3 or using level shifters.
Hi, thanks for the answer as well. Please check my answer to the previous reply, all help is very well received! :)

BikerMark
Posts: 1
Joined: Sat Dec 26, 2020 10:34 am

Re: ESP32 reading from Attiny85 sensor through I2C protocol

Postby BikerMark » Sun Dec 27, 2020 1:09 pm

Hi,

Did you manage to get it to work already?

Who is online

Users browsing this forum: No registered users and 83 guests