ESP32 reading from Attiny85 sensor through I2C protocol
Posted: 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:
I'm trying to read the current value with the ESP32 with the following code (ESP32 programmed through ESP IDF):
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
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() {
}
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));
}
}
The wiring diagram is attached.
Can you please help? What am I doing wrong?
Thanks in advance and best regards,
Tiago