Nano Every error: 'TWSR' was not declared in this scope

I am testing a RTC on an Arduino Nano Every with the code below (which works fine on an Arduino Uno), however every time I try and upload it gives the following error and exit status 1:

C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO\Adafruit_I2CDevice.cpp: In member function 'bool Adafruit_I2CDevice::setSpeed(uint32_t)':
C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO\Adafruit_I2CDevice.cpp:270:5: error: 'TWSR' was not declared in this scope
     TWSR = 0x0;
     ^~~~
C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO\Adafruit_I2CDevice.cpp:270:5: note: suggested alternative: 'TWI0'
     TWSR = 0x0;
     ^~~~
     TWI0
C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO\Adafruit_I2CDevice.cpp:274:5: error: 'TWSR' was not declared in this scope
     TWSR = 0x1;
     ^~~~
C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO\Adafruit_I2CDevice.cpp:274:5: note: suggested alternative: 'TWI0'
     TWSR = 0x1;
     ^~~~
     TWI0
C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO\Adafruit_I2CDevice.cpp:278:5: error: 'TWSR' was not declared in this scope
     TWSR = 0x2;
     ^~~~
C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO\Adafruit_I2CDevice.cpp:278:5: note: suggested alternative: 'TWI0'
     TWSR = 0x2;
     ^~~~
     TWI0
C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO\Adafruit_I2CDevice.cpp:282:5: error: 'TWSR' was not declared in this scope
     TWSR = 0x3;
     ^~~~
C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO\Adafruit_I2CDevice.cpp:282:5: note: suggested alternative: 'TWI0'
     TWSR = 0x3;
     ^~~~
     TWI0
C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO\Adafruit_I2CDevice.cpp:290:3: error: 'TWBR' was not declared in this scope
   TWBR = atwbr;
   ^~~~
C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO\Adafruit_I2CDevice.cpp:290:3: note: suggested alternative: 'TWI0'
   TWBR = atwbr;
   ^~~~
   TWI0
Multiple libraries were found for "Adafruit_I2CDevice.h"
 Used: C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO
 Not used: C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO-master

It suggested using "TWI0" as an alternative, but how would I change that? What is causing this error?
I've seen a few previous posts but none of them seemed to have a clear solution

Below is my full code:

#include <RTClib.h>
#include <Wire.h>
#include <Adafruit_I2CDevice.h>
RTC_DS1307 rtc;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  if(! rtc.begin()){
    Serial.println("Couldnt find RTC");
    while(1);
  }
  if(! rtc.isrunning()){
    Serial.println("RTC is NOT running!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  DateTime now = rtc.now();
  Serial.print(now.year(), DEC);
  Serial.print('/');
  Serial.println(now.hour(), DEC);
  Serial.print('/');
  Serial.print(now.minute(), DEC);
  Serial.print('/');
  Serial.print(now.second(), DEC);
}

Any help would be much appreciated!

Sounds like that library is not compatible with the Nano Every.

1 Like

Your topic was MOVED to its current forum category as it is more suitable than the original as it is not a bootloader or Avrdude isuue

The 2 boards have different processors and hence have different register and ports, which is almost certainly the cause of the problem

The trouble is in the function to set the speed. They should have used the Wire.setClock() function, but no, they tried to be smart :grimacing:

The trouble function is here: https://github.com/adafruit/Adafruit_BusIO/blob/master/Adafruit_I2CDevice.cpp#L263

The Nano Every has a different microcontroller, but it is part of the "AVR" family microcontrollers.

I see--does that mean the library is completely incompatible with the Nano Every then? Is there a way to use a RTC that can get around these errors?

Can you change the library ?

bool Adafruit_I2CDevice::setSpeed(uint32_t desiredclk) {
  _wire->setClock(desiredclk);
  return true;
}

It looks like code with registers of old nano.
As far as I know, nano every has a register compatibility mode with regular nano

If you mean the register emulation, I actually have that turned on (under tools, it currently is displays "Registeres emulation: ATMEGA328")

Do you mean like the below?

#include <RTClib.h>
#include <Wire.h>
#include <Adafruit_I2CDevice.h>
bool Adafruit_I2CDevice::setSpeed(uint32_t desiredclk) {
  _wire->setClock(desiredclk);
  return true;
}
RTC_DS1307 rtc;

void setup() {

I tried compiling this and it is still throwing the same error

TWSR is legal name of atmega328 TWI Status Register. If you switched register emulation, the error "TWSR was not declared" looks very strange

Yeah I'm not sure where the issue is with this--any other fixes I could try here?

I re-uploaded the code with Koepel's suggestion and instead of 'TWSR not declared' here were the errors I was getting:

In file included from C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO-master\Adafruit_I2CDevice.h:5:0,
                from C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO-master\Adafruit_I2CDevice.cpp:1:
C:\Users\Xiaow\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.7\libraries\Wire\src/Wire.h: In member function 'bool Adafruit_I2CDevice::_read(uint8_t*, size_t, bool)':
C:\Users\Xiaow\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.7\libraries\Wire\src/Wire.h:64:12: note: candidate 1: size_t TwoWire::requestFrom(int, int, int)
    size_t requestFrom(int, int, int);
           ^~~~~~~~~~~
C:\Users\Xiaow\AppData\Local\Arduino15\packages\arduino\hardware\megaavr\1.8.7\libraries\Wire\src/Wire.h:62:12: note: candidate 2: virtual size_t TwoWire::requestFrom(uint8_t, size_t, bool)
    size_t requestFrom(uint8_t, size_t, bool);
           ^~~~~~~~~~~

as well as

C:\Users\Xiaow\AppData\Local\Temp\arduino_build_433996\libraries\Adafruit_BusIO-master\Adafruit_I2CDevice.cpp.o (symbol from plugin): In function `Adafruit_I2CDevice::Adafruit_I2CDevice(unsigned char, TwoWire*)':
(.text+0x0): multiple definition of `Adafruit_I2CDevice::setSpeed(unsigned long)'
C:\Users\Xiaow\AppData\Local\Temp\arduino_build_433996\sketch\rtc_testing.ino.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Multiple libraries were found for "Adafruit_I2CDevice.h"
 Used: C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO-master
 Not used: C:\Users\Xiaow\Documents\Arduino\libraries\Adafruit_BusIO

The error message seem to alternate between this and TWSR not declared randomly...

The register emulation handles only PORTx, PINx and DDRx registers.

I'm not too familiar with registers--so does this mean that it is impossible then to use the library?

If I recall correctly, the library will compile with MegaCoreX (GitHub - MCUdude/MegaCoreX: An Arduino hardware package for ATmega4809, ATmega4808, ATmega3209, ATmega3208, ATmega1609, ATmega1608, ATmega809 and ATmega808)

An alternative is to try and use RTClib version 1.14 and decline to add the Adafruit i/o if asked. It's not clear what platforms require Adafruit Bus I/O, but Version 2.0 and later embedded the use and didn't make it optional. You can remove the Adafruit Bus I/O library if you don't need it elsewhere.

Most of the useful features of RTCLib are in 1.14.

I meant replacing the function Adafruit_I2CDevice::setSpeed() in the library files. It only has to contain a call to setClock().

You have installed the Adafruit Bus IO library twice.
Next to your project folders is a folder "libraries", probably located at "Documents / Arduino / libraries'. Remove every library that you don't need and remove all the Bus IO libraries. Then start the Arduino IDE, go to the Library Manager, and install the Adafruit Bus IO library and other libraries that you need.

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