Ds18b20 attiny13

Good day! I'm trying to create a thermostat on attiny13, ds18b20 and TM1637 display. There is a problem with the OneWire library. An error occurs during compilation:

OneWire_direct_gpio.h:14:42: error: 'digitalPinToBitMask' was not declared in this scope

 #define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))

How can I work around this problem?

Initializing my sensor:

#include "OneWire.h"
#include "DallasTemperature.h"
OneWire oneWire(0);
DallasTemperature ds(&oneWire);

Attiny13 has 1kb of code size and 64 bytes of RAM only. Therefore using the standard arduino libraries is not suitable for it. I very much doubt that there will be enough space in the Tini13 to work with the display and thermostat.

Please explain what is the reason to do it with Attiny13? What is your experience in programming in general and in developing applications for microcontrollers?

1 Like

attiny13 was chosen because it is available in large quantities, and there is also experience of games created on it. Maybe I should choose another microcontroller. Do you think atmega8 is suitable?

What's the benefit of having a lot of Tiny13 if your program won't fit inside?
You didn’t answer whether you have experience in programming microcontrollers not in Arduino, but at a low level, which is necessary for this project?

Returning to the main subject - to work with Attiny you need to install a specific Attiny-core in IDE and use a specific Attiny versions of the libraries, i.e

1 Like

I have never worked in low-level languages ​​(assembler or C), I always wrote code in arduino ide in C++. Are there any good articles to learn this?

So I recommend you first try to write the code in Arduino IDE. Did you installed an Attiny-core? Did you try to use Onewire library that I pointed?

1 Like


I copied the example, downloaded the necessary libraries, tried to compile the code, but it gave an error. What did I do wrong?

You can try this library instead and see if it solves this problem:

Also make sure that you have installed the Attiny core files and following others steps meticulously:

1 Like

I can't see anything on your pictures. Did you read a forum rules?
Please do not use a screenshots!
Insert your code and error messages AS TEXT in the forum using code tags.

1 Like
#include <avr/io.h>
#include <util/delay.h>
#include "onewire.h"

#define  DS18B20_PIN PB0
#define LED_PIN   PB1

#define TEMP_MIN  (19)
#define TEMP_MAX  (22)

uint8_t read_temperature(uint8_t pin)
{
  uint16_t t;

  onewire_reset(pin); // 1-Wire reset
  onewire_write(pin, ONEWIRE_SKIP_ROM); // to all devices on the bus
  onewire_write(pin, 0x44); // send DS18B20 command, "CONVERT T"

  onewire_reset(pin); // 1-Wire reset
  onewire_write(pin, ONEWIRE_SKIP_ROM); // to all devices on the bus
  onewire_write(pin, 0xBE); // send DS18B20 command, "READ SCRATCHPAD"

  t = onewire_read(pin); // read temperature low byte
  t |= (uint16_t)onewire_read(pin) << 8; // and high byte
  t = ((t >> 4) * 100 + ((t << 12) / 6553) * 10) / 100; // decode temp

  return (uint8_t)t;
}

int
main(void)
{
  uint8_t t;

  /* setup */
  DDRB |= _BV(LED_PIN); // set LED pin as OUTPUT

  /* loop */
  while (1) {
    t = read_temperature(DS18B20_PIN);
    if (t < TEMP_MIN || t > TEMP_MAX) {
      PORTB |= _BV(LED_PIN); // light up the alarm LED
    } else {
      PORTB &= ~_BV(LED_PIN); // turn the alarm LED off
    }
    _delay_ms(1000);
  }
}

ERROR:

`sketch\main.ino.cpp.o: In function `read_temperature(unsigned char)':

C:\Users\Андрей\Downloads\attiny-onewire-library-master\main/main.ino:15: undefined reference to `onewire_reset(unsigned char)'

C:\Users\Андрей\Downloads\attiny-onewire-library-master\main/main.ino:16: undefined reference to `onewire_write(unsigned char, unsigned char)'

C:\Users\Андрей\Downloads\attiny-onewire-library-master\main/main.ino:17: undefined reference to `onewire_write(unsigned char, unsigned char)'

C:\Users\Андрей\Downloads\attiny-onewire-library-master\main/main.ino:19: undefined reference to `onewire_reset(unsigned char)'

C:\Users\Андрей\Downloads\attiny-onewire-library-master\main/main.ino:20: undefined reference to `onewire_write(unsigned char, unsigned char)'

C:\Users\Андрей\Downloads\attiny-onewire-library-master\main/main.ino:21: undefined reference to `onewire_write(unsigned char, unsigned char)'

C:\Users\Андрей\Downloads\attiny-onewire-library-master\main/main.ino:23: undefined reference to `onewire_read(unsigned char)'

C:\Users\Андрей\Downloads\attiny-onewire-library-master\main/main.ino:24: undefined reference to `onewire_read(unsigned char)'

collect2.exe: error: ld returned 1 exit status

exit status 1
Ошибка компиляции для платы ATtiny13 (ATtiny13a).`

These threads talks about the same problem. You can check the answers:

Did you installed the library?
Why it residues in Downloads folder?

Also - using non-latin chars in Windows user name could be a problem. Try to install the IDE to the user with latin-only name

It works well with the Attiny13. Here is the temperature of the DS18B20 displayed on a TM1637 4 digit display.

I did not install the libraries but took the functions and copied it into one single tab, so I could post it here.

/**
  Attiny13 temperature meter
  Temp sensor Dallas DS18B20
  TM1637 4 digit display
*/

#include <util/delay.h>

#define DS18B20_PIN PB1
#define LED_PIN   PB0

#define TEMP_MIN  (19)
#define TEMP_MAX  (22)

#define ONEWIRE_SEARCH_ROM  0xF0
#define ONEWIRE_READ_ROM  0x33
#define ONEWIRE_MATCH_ROM 0x55
#define ONEWIRE_SKIP_ROM  0xCC
#define ONEWIRE_ALARM_SEARCH  0xEC
#define ONEWIRE_PIN_INPUT()    (DDRB &= ~_BV(pin))
#define ONEWIRE_PIN_OUTPUT()    (DDRB |= _BV(pin))
#define ONEWIRE_PIN_LOW()   (PORTB &= ~_BV(pin))
#define ONEWIRE_PIN_HIGH()    (PORTB |= _BV(pin))
#define ONEWIRE_PIN_READ()    (PINB & _BV(pin))
#define ONEWIRE_RESET_RETRIES_MAX (128)

// Main Settings
#define TM1637_DIO_PIN      PB3
#define TM1637_CLK_PIN      PB4
#define TM1637_DELAY_US     (5)
#define TM1637_BRIGHTNESS_MAX   (7)
#define TM1637_POSITION_MAX   (4)

// TM1637 commands
#define TM1637_CMD_SET_DATA   0x40
#define TM1637_CMD_SET_ADDR   0xC0
#define TM1637_CMD_SET_DSIPLAY    0x80

// TM1637 data settings (use bitwise OR to contruct complete command)
#define TM1637_SET_DATA_WRITE   0x00 // write data to the display register
#define TM1637_SET_DATA_READ    0x02 // read the key scan data
#define TM1637_SET_DATA_A_ADDR    0x00 // automatic address increment
#define TM1637_SET_DATA_F_ADDR    0x04 // fixed address
#define TM1637_SET_DATA_M_NORM    0x00 // normal mode
#define TM1637_SET_DATA_M_TEST    0x10 // test mode

// TM1637 display control command set (use bitwise OR to consruct complete command)
#define TM1637_SET_DISPLAY_OFF    0x00 // off
#define TM1637_SET_DISPLAY_ON   0x08 // on

#define  TM1637_DIO_HIGH()   (PORTB |= _BV(TM1637_DIO_PIN))
#define TM1637_DIO_LOW()    (PORTB &= ~_BV(TM1637_DIO_PIN))
#define TM1637_DIO_OUTPUT()   (DDRB |= _BV(TM1637_DIO_PIN))
#define TM1637_DIO_INPUT()    (DDRB &= ~_BV(TM1637_DIO_PIN))
#define TM1637_DIO_READ()     (((PINB & _BV(TM1637_DIO_PIN)) > 0) ? 1 : 0)
#define TM1637_CLK_HIGH()   (PORTB |= _BV(TM1637_CLK_PIN))
#define TM1637_CLK_LOW()    (PORTB &= ~_BV(TM1637_CLK_PIN))

static void TM1637_send_config(const uint8_t enable, const uint8_t brightness);
static void TM1637_send_command(const uint8_t value);
static void TM1637_start(void);
static void TM1637_stop(void);
static uint8_t TM1637_write_byte(uint8_t value);

static uint8_t _config = TM1637_SET_DISPLAY_ON | TM1637_BRIGHTNESS_MAX;
static uint8_t _segments = 0xff;
PROGMEM const uint8_t _digit2segments[] =
{
  0x3F, // 0
  0x06, // 1
  0x5B, // 2
  0x4F, // 3
  0x66, // 4
  0x6D, // 5
  0x7D, // 6
  0x07, // 7
  0x7F, // 8
  0x6F  // 9
};




/**
 * Initialize TM1637 display driver.
 * Clock pin (TM1637_CLK_PIN) and data pin (TM1637_DIO_PIN)
 * are defined at the top of this file.
 */
void TM1637_init(const uint8_t enable, const uint8_t brightness);

/**
 * Turn display on/off.
 * value: 1 - on, 0 - off
 */
void TM1637_enable(const uint8_t value);

/**
 * Set display brightness.
 * Min value: 0
 * Max value: 7
 */
void TM1637_set_brightness(const uint8_t value);

/**
 * Display raw segments at position (0x00..0x03)
 *
 *      bits:
 *        -- 0 --
 *       |       |
 *       5       1
 *       |       |
 *        -- 6 --
 *       |       |
 *       4       2
 *       |       |
 *        -- 3 --
 *
 * Example segment configurations:
 * - for character 'H', segments=0b01110110
 * - for character '-', segments=0b01000000
 * - etc.
 */
void TM1637_display_segments(const uint8_t position, const uint8_t segments);

/**
 * Display digit ('0'..'9') at position (0x00..0x03)
 */
void TM1637_display_digit(const uint8_t position, const uint8_t digit);

/**
 * Display colon on/off.
 * value: 1 - on, 0 - off
 */
void TM1637_display_colon(const uint8_t value);

/**
 * Clear all segments (including colon).
 */
void TM1637_clear(void);


int main(void) {
  uint8_t t;
  DDRB |= _BV(LED_PIN); // set LED pin as OUTPUT
  /* setup */
  TM1637_init(1/*enable*/, 5/*brightness*/);
  TM1637_display_digit(0, 10); // blank it
  //  TM1637_display_digit(1,1);
  //  TM1637_display_digit(2,10);// above 9 blanks position
  /* loop */
  while (1) {
    t = read_temperature(DS18B20_PIN);
    if (t < TEMP_MIN || t > TEMP_MAX) {
      PORTB |= _BV(LED_PIN); // light up the alarm LED
    } else {
      PORTB &= ~_BV(LED_PIN); // turn the alarm LED off
    }
    //    TM1637_display_digit(0, ((t / 1000) % 10));
    TM1637_display_digit(1, ((t / 100) % 10));
    TM1637_display_digit(2, ((t / 10) % 10));
    TM1637_display_digit(3, (t % 10));
    TM1637_display_colon(1);
    _delay_ms(200);
    circle();
    TM1637_display_colon(0);
    _delay_ms(200);
    circle();
  }
}

uint8_t read_temperature(uint8_t pin) {
  uint16_t t;
  onewire_reset(pin); // 1-Wire reset
  onewire_write(pin, ONEWIRE_SKIP_ROM); // to all devices on the bus
  onewire_write(pin, 0x44); // send DS18B20 command, "CONVERT T"

  onewire_reset(pin); // 1-Wire reset
  onewire_write(pin, ONEWIRE_SKIP_ROM); // to all devices on the bus
  onewire_write(pin, 0xBE); // send DS18B20 command, "READ SCRATCHPAD"

  t = onewire_read(pin); // read temperature low byte
  t |= (uint16_t)onewire_read(pin) << 8; // and high byte
  t = ((t >> 4) * 100 + ((t << 12) / 6553) * 10) / 100; // decode temp
  return (uint8_t)t;
}

void circle(void) {
  static uint8_t k = 1;
  TM1637_display_segments(0, k);
  k = k << 1;
  if (k > B00100000) k = 1;
}

uint8_t
onewire_reset(uint8_t pin)
{
  uint8_t retval, retries;

  ONEWIRE_PIN_LOW();
  ONEWIRE_PIN_INPUT();

  retries = ONEWIRE_RESET_RETRIES_MAX;
  while (!ONEWIRE_PIN_READ()) {
    if (--retries == 0) {
      return (2);
    }
    _delay_us(1);
  }

  ONEWIRE_PIN_OUTPUT();
  _delay_us(480);
  ONEWIRE_PIN_INPUT();
  _delay_us(66);
  retval = ONEWIRE_PIN_READ();
  _delay_us(414);

  return (retval);
}

static uint8_t onewire_bit(uint8_t pin, uint8_t value) {
  uint8_t sreg;

  sreg = SREG;
  cli();
    ONEWIRE_PIN_OUTPUT();
    _delay_us(1);
    if (value) {
        ONEWIRE_PIN_INPUT();
  }
  _delay_us(14);
  value = !(ONEWIRE_PIN_READ() == 0);
    _delay_us(45);
    ONEWIRE_PIN_INPUT();
    SREG = sreg;

  return value;
}

uint8_t onewire_write(uint8_t pin, uint8_t value) {
  uint8_t i, r;

  for (i = 0; i < 8; ++i) {
        r = onewire_bit(pin, value & 0x01);
    value >>= 1;
        if (r) {
      value |= 0x80;
    }
  }

    return value;
}

uint8_t
onewire_read(uint8_t pin)
{

  return onewire_write(pin, 0xff);
}

void TM1637_init(const uint8_t enable, const uint8_t brightness) {
  DDRB |= (_BV(TM1637_DIO_PIN) | _BV(TM1637_CLK_PIN));
  PORTB &= ~(_BV(TM1637_DIO_PIN) | _BV(TM1637_CLK_PIN));
  TM1637_send_config(enable, brightness);
}

void TM1637_enable(const uint8_t value) {
  TM1637_send_config(value, _config & TM1637_BRIGHTNESS_MAX);
}

void TM1637_set_brightness(const uint8_t value) {
  TM1637_send_config(_config & TM1637_SET_DISPLAY_ON, value & TM1637_BRIGHTNESS_MAX);
}

void TM1637_display_segments(const uint8_t position, const uint8_t segments) {
  TM1637_send_command(TM1637_CMD_SET_DATA | TM1637_SET_DATA_F_ADDR);
  TM1637_start();
  TM1637_write_byte(TM1637_CMD_SET_ADDR | (position & (TM1637_POSITION_MAX - 1)));
  TM1637_write_byte(segments);
  TM1637_stop();
}

void TM1637_display_digit(const uint8_t position, const uint8_t digit) {
  uint8_t segments = (digit < 10 ? pgm_read_byte_near((uint8_t *)&_digit2segments + digit) : 0x00);

  if (position == 0x01) {
    segments = segments | (_segments & 0x80);
    _segments = segments;
  }

  TM1637_display_segments(position, segments);
}

void TM1637_display_colon(const uint8_t value) {

  if (value) {
    _segments |= 0x80;
  } else {
    _segments &= ~0x80;
  }
  TM1637_display_segments(0x01, _segments);
}

void TM1637_clear(void) {
  uint8_t i;

  for (i = 0; i < TM1637_POSITION_MAX; ++i) {
    TM1637_display_segments(i, 0x00);
  }
}

void TM1637_send_config(const uint8_t enable, const uint8_t brightness) {

  _config = (enable ? TM1637_SET_DISPLAY_ON : TM1637_SET_DISPLAY_OFF) |
            (brightness > TM1637_BRIGHTNESS_MAX ? TM1637_BRIGHTNESS_MAX : brightness);

  TM1637_send_command(TM1637_CMD_SET_DSIPLAY | _config);
}

void TM1637_send_command(const uint8_t value) {
  TM1637_start();
  TM1637_write_byte(value);
  TM1637_stop();
}

void TM1637_start(void) {
  TM1637_DIO_HIGH();
  TM1637_CLK_HIGH();
  _delay_us(TM1637_DELAY_US);
  TM1637_DIO_LOW();
}

void TM1637_stop(void) {
  TM1637_CLK_LOW();
  _delay_us(TM1637_DELAY_US);

  TM1637_DIO_LOW();
  _delay_us(TM1637_DELAY_US);

  TM1637_CLK_HIGH();
  _delay_us(TM1637_DELAY_US);

  TM1637_DIO_HIGH();
}

uint8_t TM1637_write_byte(uint8_t value) {
  uint8_t i, ack;

  for (i = 0; i < 8; ++i, value >>= 1) {
    TM1637_CLK_LOW();
    _delay_us(TM1637_DELAY_US);

    if (value & 0x01) {
      TM1637_DIO_HIGH();
    } else {
      TM1637_DIO_LOW();
    }

    TM1637_CLK_HIGH();
    _delay_us(TM1637_DELAY_US);
  }

  TM1637_CLK_LOW();
  TM1637_DIO_INPUT();
  TM1637_DIO_HIGH();
  _delay_us(TM1637_DELAY_US);

  ack = TM1637_DIO_READ();
  if (ack) {
    TM1637_DIO_OUTPUT();
    TM1637_DIO_LOW();
  }
  _delay_us(TM1637_DELAY_US);

  TM1637_CLK_HIGH();
  _delay_us(TM1637_DELAY_US);

  TM1637_CLK_LOW();
  _delay_us(TM1637_DELAY_US);

  TM1637_DIO_OUTPUT();

  return ack;
}
Sketch uses 754 bytes (73%) of program storage space. Maximum is 1024 bytes.
Global variables use 2 bytes (3%) of dynamic memory, leaving 62 bytes for local variables. Maximum is 64 bytes.

You can go with Digispark ATtiny85 Dev Board (6 KB user Flash).
DigisparkAttiny85Board
Figure-1:

Aatiny85 Dev Board
Figure-2: Alternative of Fig-1

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.