Go Down

Topic: Arduino Due & The Wire Library (Read 9618 times) previous topic - next topic


Nov 09, 2017, 04:31 am Last Edit: Nov 09, 2017, 04:33 am by GolamMostafa
@ pert  

This is just to satisfy my queries:

I have found two Wire.h files in the following paths in my one PC (rarely used when my primary PC is down). The contents of the files look identical. I have also included the .cpp files.

This PC > Documents > WPA Files > Arduino15 > packages > arduino > hardware > sam > 1.6.2 > libraries > Wire

This PC > Local Disk (C:) > Program Files(x86) > Arduino > hardware > arduino > avr > libraries > Wire > src

In my other two PCs, the Top path (.....sam > ) does not exist.

In all these 3 computers, TWI Bus DUE and UNO programs run well.


In my other two PCs, the Top path (.....sam > ) does not exist.
Are you saying that on those two PCs if you do this:
  • Tools > Board > Arduino Due
  • File > Examples > Wire > digital_potentiometer
  • Sketch > Show sketch folder

It opens this exact path:
This PC > Local Disk (C:) > Program Files(x86) > Arduino > hardware > arduino > avr > libraries > Wire > src


From https://github.com/arduino/ArduinoCore-sam/issues/19:Almost certainly not. Try this:
  • Tools > Board > Arduino/Genuino Uno
  • File > Examples > Wire > digital_potentiometer
  • Sketch > Show sketch folder
  • Tools > Board > Arduino Due
  • File > Examples > Wire > digital_potentiometer
  • Sketch > Show sketch folder

You will now see that you have two different Wire folders. One is in the libraries subfolder of your Arduino AVR Boards hardware package, the other in the libraries subfolder of your Arduino SAM Boards package. It's easy to think it's only one library because of the standardized API and the IDE automatically choosing the correct library based on board selection.


Earlier today I found at least 4 different Wire Libraries

the Boards
Arduino Yun - /home/niemand/arduino-1.8.5/hardware/arduino/avr/libraries/Wire/examples/digital_potentiometer/
Arduino Due - /home/niemand/.arduino15/packages/arduino/hardware/sam/1.6.11/libraries/Wire/examples/digital_potentiometer/
Arduino Zero - /home/niemand/.arduino15/packages/arduino/hardware/samd/1.6.16/libraries/Wire/examples/digital_potentiometer/
Arduino Primo - /home/niemand/.arduino15/packages/arduino/hardware/nrf52/1.0.2/libraries/Wire/examples/digital_potentiometer/

So with the limited buffer in Arduino I2C/TWI the two Arduino Due master / slave example does work.

I did come across another interesting note on the Wire Library:


There are both 7- and 8-bit versions of I2C addresses. 7 bits identify the device, and the eighth bit determines if it's being written to or read from. The Wire library uses 7 bit addresses throughout. If you have a datasheet or sample code that uses 8 bit address, you'll want to drop the low bit (i.e. shift the value one bit to the right), yielding an address between 0 and 127. However the addresses from 0 to 7 are not used because are reserved so the first address that can be used is 8. Please note that a pull-up resistor is needed when connecting SDA/SCL pins. Please refer to the examples for more informations. MEGA 2560 board has pull-up resistors on pins 20 - 21 onboard.
Are 0 through 7 still reserved?
I did change the 2 in the examples to 8 just in case.



I had a similar problem so I wrote my own library for the TWI. But I have only implemented what I needed in my project. Here's the link: https://github.com/hirschmensch/Arduino-Due-DSP

sam3x8e_twi is the name of the library files.

maybe this helps...


I don't know if this helps but I found that I believe that the include directory for the HID, SPI, and Wire  files end at Sam/1.6.11/libraries . I would think that as an example that should include Wire/src in the list. When I did this in Visual Studio the problem seams to go away any input?


Nov 28, 2018, 09:14 pm Last Edit: Nov 28, 2018, 10:03 pm by hive-o
Perhaps, if working directly with Wire.something one can easily edit scripts to permit Wire1.something to operate on SDA1 and SCL1 connected devices.

Yet, if working with device libraries, that ain't directly commutable...

I have been able to activate SDA1 and SCL1 operation on an Arduino DUE with a BME280 library: https://github.com/sparkfun/SparkFun_BME280_Arduino_Library  While this edit doesn't permit simultaneous operation of both I2C buses, it demonstrates the enabling of a library for SDA1 and SCL1 use.

Note that BME280.h and BME280.cpp require the following edits to make this script operate with SDA1 and SCL1 on the I2C bus
Edit line 181 of BME280.h from "Wire" to "Wire1"
Edit line 39 of BME280.cpp from "Wire" to "Wire1"
Also, remember to add pull-up resistors to 3.3V for both SDA1 and SCL1. Here, I am using 2.2k on each.


Hi, Sorry for my bad English, I speak Russian.
There is a problem with the Wire.endTransmission (stop) function for Arduino DUE, this function ignores the stop argument, when I send the Wire.endTransmission (false) command, the Wire.endTransmission command is still executed (true), this makes it impossible to use restart and subsequent command Wire.requestFrom ();
I looked into the library wire.cpp

Code: [Select]
uint8_t TwoWire::endTransmission(uint8_t sendStop) {
uint8_t error = 0;
// transmit buffer (blocking)
TWI_StartWrite(twi, txAddress, 0, 0, txBuffer[0]);
if (!TWI_WaitByteSent(twi, XMIT_TIMEOUT))
error = 2; // error, got NACK on address transmit

if (error == 0) {
uint16_t sent = 1;
while (sent < txBufferLength) {
TWI_WriteByte(twi, txBuffer[sent++]);
if (!TWI_WaitByteSent(twi, XMIT_TIMEOUT))
error = 3; // error, got NACK during data transmmit

if (error == 0) {

if (!TWI_WaitTransferComplete(twi, XMIT_TIMEOUT))
error = 4; // error, finishing up


txBufferLength = 0; // empty buffer
status = MASTER_IDLE;
return error;

 And what we see is that the argument uint8_t sendStop  in the function itself it is not used where it is, it is a fake.
How to fix this function so that it works just as well as on AVR microcontrollers?

Go Up