RobTillaart / SHT85

Arduino library for the SHT85 temperature and humidity sensor
MIT License
11 stars 4 forks source link

How to define clock and data pin manually without using default i2c pin #16

Closed MFahreza69 closed 1 year ago

MFahreza69 commented 1 year ago

hai, i am new in microcontroller programming i have a question how can i define clock and data pin manually, iam using atmega64 for the cores, and want to connect the data pin to PD6 and clockpin to PD7. is that possible? thank you for your time

RobTillaart commented 1 year ago

Thanks for the issue,

I am not familiar with the atmega64. I do not see it under the AVR boards in the Arduino IDE. Do you have a link?

MFahreza69 commented 1 year ago

thank you for your answer, here's the link i download the library board of atmega64 (megacore family)

https://github.com/MCUdude/MegaCore

i just want to know is that possible if i can change the i2c pin with other pin for the communication sht sensor, because in my pcb the i2c pin was connected to RTCmodule

RobTillaart commented 1 year ago

Check

where pins are defined. You might try to change them there.

RobTillaart commented 1 year ago

The I2C is a bus, that means multiple devices can use the same 2 pins for SDA and SCL. Every device has an address (often configurable) that may not be the same of course.

So you should just connect the SHT85 to the same pins as the RTC. Be careful and check the connections very well as the SHT85 is "sensitive" => I fried a SHT85 during a quick test.

RobTillaart commented 1 year ago

An alternative might be to use a software I2C library.

Recently I created this class that should work for the SHT85 too - https://github.com/RobTillaart/SHT31_SW You need to install my SHT31 library (base class) and some more.

MFahreza69 commented 1 year ago

thank you for your advice, in my pcb the the connection for i2c pin was fixed so i can't jumper it so it is alright but i will try the second method for using the software I2C

im very thankful and gratitude to you, hope you have a great day Rob :D

RobTillaart commented 1 year ago

Any progress with the SW I2C ?

RobTillaart commented 1 year ago

As the issue seems solved I close this issue.

MFahreza69 commented 1 year ago

hy Rob sorry i have a problem for buying the sht85 maybe i must wait until next month, but in software the code can compiled clearly thank you

RobTillaart commented 1 year ago

Then I close this issue for now, but feel free to reopen when you have a sensor. (oops, clicked open by accident)

MFahreza69 commented 1 year ago

Then I close this issue for now, but feel free to reopen when you have a sensor. (oops, clicked open by accident)

okay, thank you Rob

MFahreza69 commented 1 year ago

hai Rob, now i have the sensor(SHT85) i use the SHT31_SW library and the example code that you provide, i connect the sda pin to pin 6 and scl to pin 7 (On Arduino Mega as experiment) but the sensor did not give the right result of temperature and humidity, the read result was temp = -45.0 and humidity = 0.0 So it means that the sensor was not supported for using Software I2c?

RobTillaart commented 1 year ago

I would not expect that, do not recall testing that combination.

If you call isConnected() what is returned? Does the sensor work on the hardware I2C bus of the MEGA? Have you tried other devices on the SW I2C?

MFahreza69 commented 1 year ago

here is the picture of my serial monitor

Screenshot 2023-05-04 101035

actually i upload the SHT31_isConnected.ino code, the sensor work in hardware I2C on Mega at pin 20-21 i haven't try other device on the sw I2c before.

RobTillaart commented 1 year ago

(please copy output as text next time)

The lines 1 ==> file name 2 ==> version library 3 ==> sht.readStatus(); == 0xFFFF so the data line is constant held HIGH

then the columns are

So no clues, to investigate I need to build a test setup, however I do not have an ATMEGA64. (=> ends investigation)

MFahreza69 commented 1 year ago

Just now i test the sht85 using your SHT31 library (hardware I2c) and it works normally. Can i ask something? in your test before, what micro u use and any pin that should i use or avoid in SW I2c, maybe not all pin can be modified to SW I2c, for now i will be using arduino Mega but still use the SW I2C, and the last, maybe i forgot some procedure to execute this code like i forgot to add library or adding something? Thank you for your time

RobTillaart commented 1 year ago

Just now i test the sht85 using your SHT31 library (hardware I2c) and it works normally.

that's good to hear, sensor is OK

in your test before, what micro u use

I use UNO mostly, and pretty sure I used it for these tests too. Many boards e.g. ESP32 can define the pins of their I2C so SW_I2C makes less sense.


and any pin that should i use or avoid in SW I2c,

no Idea, you need to read data sheets and the code of the SoftWire library for that, at least you best avoid pins that have a clear function e.g Serial ports


You are using ATMEGA64 ?

RobTillaart commented 1 year ago

@MFahreza69 Did a test with an SHT85 and I can confirm your observations/measurements SHT85 work well with SHT31 library, but not with SHT31_SW. Created an issue for that under SHT31_SW - where it belongs.

MFahreza69 commented 1 year ago

@MFahreza69 Did a test with an SHT85 and I can confirm your observations/measurements SHT85 work well with SHT31 library, but not with SHT31_SW. Created an issue for that under SHT31_SW - where it belongs.

Hai Rob, sorry i had a headache and must to go to hospital yesterday i have already read the issue in the SHT31_SW do you want to improve and edit your library?

RobTillaart commented 1 year ago

do you want to improve and edit your library?

Improvement is always good (assumption).

At this moment I have no clue why the SHT85 does not work with the SHT31_SW.

MFahreza69 commented 1 year ago

I test the sensor using softWire.h and uploading the"ListDevice.ino" file to check my softwire I2c and the serial monitor print the device in all I2c address, it should print only the available device or the device that connect to the pin right? i mean if i connect the sht85 to pin 6-7(sda-scl) this code should print an available address only at 0x44/0x45 because i only connect the sht48.

but in my device(atmega2560-arduino Mega) it prints all of the available device and address

17:14:04.030 -> Interrogating all addresses in range 0x1 - 0x7F (inclusive) ... 17:14:04.124 -> Device found at 0x1 17:14:04.265 -> Device found at 0x2 17:14:04.405 -> Device found at 0x3 17:14:04.546 -> Device found at 0x4 17:14:04.687 -> Device found at 0x5 17:14:04.827 -> Device found at 0x6 17:14:04.968 -> Device found at 0x7 17:14:05.109 -> Device found at 0x8 17:14:05.250 -> Device found at 0x9 17:14:05.391 -> Device found at 0xA 17:14:05.532 -> Device found at 0xB 17:14:05.672 -> Device found at 0xC 17:14:05.813 -> Device found at 0xD 17:14:05.953 -> Device found at 0xE 17:14:06.094 -> Device found at 0xF 17:14:06.235 -> Device found at 0x10 17:14:06.376 -> Device found at 0x11 17:14:06.516 -> Device found at 0x12 17:14:06.657 -> Device found at 0x13 17:14:06.802 -> Device found at 0x14 17:14:06.896 -> Device found at 0x15 17:14:07.038 -> Device found at 0x16 17:14:07.178 -> Device found at 0x17 17:14:07.319 -> Device found at 0x18 17:14:07.460 -> Device found at 0x19 17:14:07.600 -> Device found at 0x1A 17:14:07.741 -> Device found at 0x1B 17:14:07.882 -> Device found at 0x1C 17:14:08.042 -> Device found at 0x1D 17:14:08.171 -> Device found at 0x1E 17:14:08.322 -> Device found at 0x1F 17:14:08.467 -> Device found at 0x20 17:14:08.611 -> Device found at 0x21 17:14:08.737 -> Device found at 0x22 17:14:08.860 -> Device found at 0x23 17:14:09.000 -> Device found at 0x24 17:14:09.144 -> Device found at 0x25 17:14:09.286 -> Device found at 0x26 17:14:09.427 -> Device found at 0x27 17:14:09.568 -> Device found at 0x28 17:14:09.709 -> Device found at 0x29 17:14:09.850 -> Device found at 0x2A 17:14:09.990 -> Device found at 0x2B 17:14:10.131 -> Device found at 0x2C 17:14:10.272 -> Device found at 0x2D 17:14:10.412 -> Device found at 0x2E 17:14:10.553 -> Device found at 0x2F 17:14:10.693 -> Device found at 0x30 17:14:10.839 -> Device found at 0x31 17:14:10.980 -> Device found at 0x32 17:14:11.121 -> Device found at 0x33 17:14:11.262 -> Device found at 0x34 17:14:11.404 -> Device found at 0x35 17:14:11.544 -> Device found at 0x36 17:14:11.685 -> Device found at 0x37 17:14:11.826 -> Device found at 0x38 17:14:11.966 -> Device found at 0x39 17:14:12.107 -> Device found at 0x3A 17:14:12.248 -> Device found at 0x3B 17:14:12.377 -> Device found at 0x3C 17:14:12.517 -> Device found at 0x3D 17:14:12.658 -> Device found at 0x3E 17:14:12.799 -> Device found at 0x3F 17:14:12.940 -> Device found at 0x40 17:14:13.080 -> Device found at 0x41 17:14:13.222 -> Device found at 0x42 17:14:13.363 -> Device found at 0x43 17:14:13.504 -> Device found at 0x44 17:14:13.644 -> Device found at 0x45 17:14:13.785 -> Device found at 0x46 17:14:13.926 -> Device found at 0x47 17:14:14.067 -> Device found at 0x48 17:14:14.208 -> Device found at 0x49 17:14:14.349 -> Device found at 0x4A 17:14:14.491 -> Device found at 0x4B 17:14:14.631 -> Device found at 0x4C 17:14:14.772 -> Device found at 0x4D 17:14:14.913 -> Device found at 0x4E 17:14:15.055 -> Device found at 0x4F 17:14:15.196 -> Device found at 0x50 17:14:15.336 -> Device found at 0x51 17:14:15.477 -> Device found at 0x52 17:14:15.618 -> Device found at 0x53 17:14:15.759 -> Device found at 0x54 17:14:15.899 -> Device found at 0x55 17:14:16.040 -> Device found at 0x56 17:14:16.167 -> Device found at 0x57 17:14:16.308 -> Device found at 0x58 17:14:16.448 -> Device found at 0x59 17:14:16.589 -> Device found at 0x5A 17:14:16.730 -> Device found at 0x5B 17:14:16.870 -> Device found at 0x5C 17:14:17.011 -> Device found at 0x5D 17:14:17.152 -> Device found at 0x5E 17:14:17.292 -> Device found at 0x5F 17:14:17.433 -> Device found at 0x60 17:14:17.575 -> Device found at 0x61 17:14:17.716 -> Device found at 0x62 17:14:17.856 -> Device found at 0x63 17:14:17.997 -> Device found at 0x64 17:14:18.137 -> Device found at 0x65 17:14:18.278 -> Device found at 0x66 17:14:18.419 -> Device found at 0x67 17:14:18.559 -> Device found at 0x68 17:14:18.700 -> Device found at 0x69 17:14:18.794 -> Device found at 0x6A 17:14:18.935 -> Device found at 0x6B 17:14:19.076 -> Device found at 0x6C 17:14:19.216 -> Device found at 0x6D 17:14:19.357 -> Device found at 0x6E 17:14:19.497 -> Device found at 0x6F 17:14:19.638 -> Device found at 0x70 17:14:19.779 -> Device found at 0x71 17:14:19.919 -> Device found at 0x72 17:14:20.060 -> Device found at 0x73 17:14:20.200 -> Device found at 0x74 17:14:20.341 -> Device found at 0x75 17:14:20.483 -> Device found at 0x76 17:14:20.624 -> Device found at 0x77 17:14:20.765 -> Device found at 0x78 17:14:20.905 -> Device found at 0x79 17:14:21.046 -> Device found at 0x7A 17:14:21.187 -> Device found at 0x7B 17:14:21.327 -> Device found at 0x7C 17:14:21.468 -> Device found at 0x7D 17:14:21.608 -> Device found at 0x7E 17:14:21.749 -> Device found at 0x7F 17:14:21.796 -> Finished

RobTillaart commented 1 year ago

(such long logs do not improve readability, you better shorten them to make your point)

but in my device(atmega2560-arduino Mega) it prints all of the available device and address

So your MEGA sees more devices than there are, even with no device connected? Can you check if this problem also occurs with an UNO?

Could it be that the SOftwareI2C library is not MEGA compatible?

MFahreza69 commented 1 year ago

(such long logs do not improve readability, you better shorten them to make your point)

but in my device(atmega2560-arduino Mega) it prints all of the available device and address

So your MEGA sees more devices than there are, even with no device connected? Can you check if this problem also occurs with an UNO?

Could it be that the SOftwareI2C library is not MEGA compatible?

ok i'll check the softWire.h to UNO and the sensors too

Compatible or not, i dont know because it's my first time using software I2C :D but ya hope we can find the answer

MFahreza69 commented 1 year ago

do you mind if i edit your library? i change the softWire.h with softwareWire.h and it works, the sensor shows the temp and humidity value in sht31_SW library i test in atmega 64 on pin 24 and 25 (with pullup resistor 10k in each pin)

09:19:54.203 -> SHT31_SW_LIB_VERSION: 0.1.0 09:19:54.203 -> 8010 09:19:54.203 -> 18 5600 1 0 8010 29.3 60.9 09:19:55.219 -> 1026 5576 1 0 8010 29.2 60.9 09:19:56.203 -> 2034 5580 1 0 8010 29.2 61.0 09:19:57.235 -> 3043 5580 1 0 8010 29.2 61.0 09:19:58.219 -> 4051 5584 1 0 8010 29.2 60.9 09:19:59.251 -> 5059 5576 1 0 8010 29.3 60.9 09:20:00.273 -> 6068 5572 1 0 8010 29.2 60.8 09:20:01.257 -> 7076 5584 1 0 8010 29.2 60.8 09:20:02.266 -> 8085 5584 1 0 8010 29.2 61.0 09:20:03.298 -> 9093 5580 1 0 8010 29.2 61.0 09:20:04.283 -> 10101 5572 1 0 8010 29.2 60.9 09:20:05.314 -> 11110 5584 1 0 8010 29.2 61.0 09:20:06.298 -> 12119 5584 1 0 8010 29.2 61.1 09:20:07.330 -> 13127 5580 1 0 8010 29.2 61.1 09:20:08.325 -> 14135 5576 1 0 8010 29.2 61.0 09:20:09.347 -> 15143 5572 1 0 8010 29.2 61.1 09:20:10.332 -> 16152 5584 1 0 8010 29.2 61.1 09:20:11.364 -> 17161 5584 1 0 8010 29.2 61.0 09:20:12.349 -> 18168 5572 1 0 8010 29.2 61.0 09:20:13.368 -> 19177 5576 1 0 8010 29.2 61.0 09:20:14.352 -> 20186 5584 1 0 8010 29.2 60.9 09:20:15.384 -> 21194 5584 1 0 8010 29.3 61.0 09:20:16.371 -> 22203 5584 1 0 8010 29.2 61.0

RobTillaart commented 1 year ago

i change the softWire.h with softwareWire.h

Think you changed it in several places, I will try to confirm today.

MFahreza69 commented 1 year ago

yes i mean, i change your library in my pc not in your repository :')

RobTillaart commented 1 year ago

yes i mean, i change your library in my pc not in your repository :')

I understand, but it was more than just changing the name of the include file.

MFahreza69 commented 1 year ago

ohh yes, but the command for sofwareWire.h is like softWire.h, https://github.com/Testato/SoftwareWire/blob/master/SoftwareWire.h

so no need to many change for the library

MFahreza69 commented 1 year ago

the .h file

`#pragma once // // FILE: SHT31_SW.h // AUTHOR: Rob Tillaart, Gunter Haug // VERSION: 0.1.0 // DATE: 2019-02-08 (base SHT31 lib) // PURPOSE: Arduino library for the SHT31 temperature and humidity sensor // to be used with the SoftWire library instead of (hardware) Wire. // derives from SHT31 0.3.8 // https://www.adafruit.com/product/2857 // URL: https://github.com/RobTillaart/SHT31_SW // URL: https://github.com/RobTillaart/SHT31

include "Arduino.h"

include

include "SHT31.h"

define SHT31_SW_LIB_VERSION (F("0.1.0"))

class SHT31_SW : public SHT31 { public: SHT31_SW();

// use SHT_DEFAULT_ADDRESS bool begin(const uint8_t address, SoftwareWire wire); bool begin(SoftwareWire wire);

// check sensor is reachable over I2C bool isConnected();

private: bool writeCmd(uint16_t cmd); bool readBytes(uint8_t n, uint8_t val); SoftwareWire _softWire; };

// -- END OF FILE --

`

the .cpp file `// // FILE: SHT31_SW.cpp // AUTHOR: Rob Tillaart // VERSION: 0.1.0 // DATE: 2019-02-08 (base SHT31 lib) // PURPOSE: Arduino library for the SHT31 temperature and humidity sensor // to be used with the SoftWire library instead of (hardware) Wire. // derives from SHT31 0.3.8 // https://www.adafruit.com/product/2857 // URL: https://github.com/RobTillaart/SHT31_SW // URL: https://github.com/RobTillaart/SHT31

include "SHT31_SW.h"

// SUPPORTED COMMANDS - single shot mode only

define SHT31_READ_STATUS 0xF32D

define SHT31_CLEAR_STATUS 0x3041

define SHT31_SOFT_RESET 0x30A2

define SHT31_HARD_RESET 0x0006

define SHT31_MEASUREMENT_FAST 0x2416 // page 10 datasheet

define SHT31_MEASUREMENT_SLOW 0x2400 // no clock stretching

define SHT31_HEAT_ON 0x306D

define SHT31_HEAT_OFF 0x3066

define SHT31_HEATER_TIMEOUT 180000UL // milliseconds

SHT31_SW::SHT31_SW() { _softWire = NULL; _address = 0; _lastRead = 0; _rawTemperature = 0; _rawHumidity = 0; _heatTimeout = 0; _heaterStart = 0; _heaterStop = 0; _heaterOn = false; _error = SHT31_OK; }

bool SHT31_SW::begin(const uint8_t address, SoftwareWire *softWire) { if ((address != 0x44) && (address != 0x45)) { return false; } _address = address; _softWire = softWire; _softWire->begin(); return reset(); }

bool SHT31_SW::begin(SoftwareWire *softWire) { return begin(SHT_DEFAULT_ADDRESS, softWire); }

bool SHT31_SW::isConnected() { _softWire->beginTransmission(_address); int rv = _softWire->endTransmission(); if (rv != 0) _error = SHT31_ERR_NOT_CONNECT; return (rv == 0); }

//////////////////////////////////////////////////// // // PRIVATE // bool SHT31_SW::writeCmd(uint16_t cmd) { _softWire->beginTransmission(_address); _softWire->write(cmd >> 8 ); _softWire->write(cmd & 0xFF); if (_softWire->endTransmission() != 0) { _error = SHT31_ERR_WRITECMD; return false; } return true; }

bool SHT31_SW::readBytes(uint8_t n, uint8_t *val) { int rv = _softWire->requestFrom(_address, (uint8_t) n); if (rv == n) { for (uint8_t i = 0; i < n; i++) { val[i] = _softWire->read(); } return true; } _error = SHT31_ERR_READBYTES; return false; }

// -- END OF FILE --

`

RobTillaart commented 1 year ago

Yep was piece of cake to patch, I am working on a 0.2.0 release for the SHT31_SW. See - https://github.com/RobTillaart/SHT31_SW/issues/5

MFahreza69 commented 1 year ago

okay Rob, thank you for the library now i can continue my works that has been delayed for a couple weeks haha :')

RobTillaart commented 1 year ago

Thanks for all this raw data, appreciate you want to help, but I am already two steps further. (when posting code use markdown code tags for readability please).

SanSanysch commented 1 year ago

Sorry, to i get into someone else's topic, but I want to confirm: SHT31 don't work on atmega 2560 and UNO. Not on pins 6, 7, not 1,2... 3,4 and other. I use example from library. Waiting v.0.2.0 Example "ListDevices" from SoftWire, see sensors properly.

RobTillaart commented 1 year ago

@SanSanysch Thanks for this listdevices tip. I will look into it coming week if time permits.

RobTillaart commented 1 year ago

Some quick tests (found minimal time slot before dinner)

My SHT31 responds with listDevices however SHT85 does not

17:35:41.915 -> Interrogating all addresses in range 0x40 - 0x50 (inclusive) ...
17:35:41.915 -> ....
Device found at 0x44
17:35:42.196 -> ............Finished

SHT31_SW using SoftWire does not work - at least not today :( - nor SHT31 nor SHT85

SHT31_SW using SoftwareWire works with SHT31 (UNO)

17:53:45.418 -> SHT31_SW_LIB_VERSION:   0.1.1
17:53:45.418 -> CON:    1
17:53:45.512 -> 8010
17:53:45.512 ->     5580    21.2    51.6
17:53:48.511 ->     5552    21.2    51.7
17:53:51.507 ->     5556    21.2    51.8
17:53:54.553 ->     5556    21.2    51.7
17:53:57.518 ->     5564    21.2    51.8

And with SHT85 (UNO)

17:57:07.436 -> SHT31_SW_LIB_VERSION:   0.1.1
17:57:07.436 -> CON:    1
17:57:07.529 -> 8010
17:57:07.529 ->     5580    21.4    49.1
17:57:10.529 ->     5548    21.4    49.1
17:57:13.528 ->     5552    21.3    49.3
17:57:16.574 ->     5548    21.4    49.1

Drawback(?) is that SoftwareWire is AVR Specific.