Hi everyone,
I recently came across the new AHT20 Temperature sensor. I want to use this specific sensor along with the Arduino in order to make a GUI in MATLAB. That's why I want to know how to properly set up the I2C communication without using the dedicated library (<AHTx...>).
The datasheet is very abstract and I am finding it hard to find my way around. Could anyone please help me figure this out.
P.S : please find the attached datasheet.
AHT20 I2C interfacing (MATLAB PROJECT)
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- Dass_Sid
- Posts: 5
- Joined: Wed Apr 21, 2021 2:08 pm
AHT20 I2C interfacing (MATLAB PROJECT)
- Attachments
-
- Datasheet-2.PNG (198.95 KiB) Viewed 275 times
-
- Datasheet-1.PNG (227.38 KiB) Viewed 275 times
- adafruit_support_carter
- Posts: 29168
- Joined: Tue Nov 29, 2016 2:45 pm
Re: AHT20 I2C interfacing (MATLAB PROJECT)
Is it required to have Arduino in the mix? Take a look at these options which may provide a more direct way to interface things into the PC:
https://learn.adafruit.com/circuitpytho ... th-mcp2221
https://learn.adafruit.com/circuitpytho ... ith-ft232h
https://learn.adafruit.com/circuitpytho ... th-mcp2221
https://learn.adafruit.com/circuitpytho ... ith-ft232h
- Dass_Sid
- Posts: 5
- Joined: Wed Apr 21, 2021 2:08 pm
Re: AHT20 I2C interfacing (MATLAB PROJECT)
Thank you for the reply. Can circuit python be used in the MATLAB App designer ?adafruit_support_carter wrote:Is it required to have Arduino in the mix? Take a look at these options which may provide a more direct way to interface things into the PC:
https://learn.adafruit.com/circuitpytho ... th-mcp2221
https://learn.adafruit.com/circuitpytho ... ith-ft232h
- adafruit_support_carter
- Posts: 29168
- Joined: Tue Nov 29, 2016 2:45 pm
Re: AHT20 I2C interfacing (MATLAB PROJECT)
Does MATLAB App designer allow interfacing with Python code?
- millercommamatt
- Posts: 832
- Joined: Tue Jul 31, 2018 4:57 pm
Re: AHT20 I2C interfacing (MATLAB PROJECT)
Let's start with getting Matlab to read the temperature from the sensor. (BTW, I saw your post on the Matlab forums)
Matlab lets you read I2C devices by directly reading the registers. You'll have to consult the sensor datasheet to figure out which registers and what to do with the output.
See this example to see the process: https://www.mathworks.com/help/supportp ... dware.html
For the code below, I'm guessing based on the data sheet page 8 and the matlab example above.
create your adruino object (a)
then:
I'd translate this code to matlab if you want a full featured library: https://github.com/adafruit/Adafruit_Ci ... t_ahtx0.py
Matlab lets you read I2C devices by directly reading the registers. You'll have to consult the sensor datasheet to figure out which registers and what to do with the output.
See this example to see the process: https://www.mathworks.com/help/supportp ... dware.html
For the code below, I'm guessing based on the data sheet page 8 and the matlab example above.
create your adruino object (a)
then:
Code: Select all
aht20 = device(a,'I2CAddress',0x38);
%trigger the measurement
write(aht20, [0xAC, 0x33, 0x00], 'uint8');
%wait 80 ms and read the measurement
pause(0.08);
raw_sensor_data = read(aht20, 7, 'uint8'); % should be 7 bytes (1-state, 2-humidity, 1-humidity temperature, 2-temperature, 1-CRC
raw_humidity = bitshift(uint32(raw_sensor_data (2)),12) + bitshift(uint32(raw_sensor_data (3)),4) + bitshift(uint32(raw_sensor_data (4)),-4);
raw_temperature = bitshift(bitand(uint32(raw_sensor_data (4)), 0xF),16) + bitshift(uint32(raw_sensor_data (5)),8) + uint32(raw_sensor_data (6));
humidity = (raw_humidity * 100) / 2^20;
temperature = ((raw_temperature * 200.0) / 2^20) - 50;
- Dass_Sid
- Posts: 5
- Joined: Wed Apr 21, 2021 2:08 pm
Re: AHT20 I2C interfacing (MATLAB PROJECT)
millercommamatt wrote:Let's start with getting Matlab to read the temperature from the sensor. (BTW, I saw your post on the Matlab forums)
Matlab lets you read I2C devices by directly reading the registers. You'll have to consult the sensor datasheet to figure out which registers and what to do with the output.
See this example to see the process: https://www.mathworks.com/help/supportp ... dware.html
For the code below, I'm guessing based on the data sheet page 8 and the matlab example above.
create your adruino object (a)
then:
I'd translate this code to matlab if you want a full featured library: https://github.com/adafruit/Adafruit_Ci ... t_ahtx0.pyCode: Select all
aht20 = device(a,'I2CAddress',0x38); %trigger the measurement write(aht20, [0xAC, 0x33, 0x00], 'uint8'); %wait 80 ms and read the measurement pause(0.08); raw_sensor_data = read(aht20, 7, 'uint8'); % should be 7 bytes (1-state, 2-humidity, 1-humidity temperature, 2-temperature, 1-CRC raw_humidity = bitshift(uint32(raw_sensor_data (2)),12) + bitshift(uint32(raw_sensor_data (3)),4) + bitshift(uint32(raw_sensor_data (4)),-4); raw_temperature = bitshift(bitand(uint32(raw_sensor_data (4)), 0xF),16) + bitshift(uint32(raw_sensor_data (5)),8) + uint32(raw_sensor_data (6)); humidity = (raw_humidity * 100) / 2^20; temperature = ((raw_temperature * 200.0) / 2^20) - 50;
Thank you very much ! This cool way of passing the commands as an array is out of the box. Thank you for your valuable input. I was able to get the data from the sensor but the conversion was an issue. Now I think I'll be able to do it. Thanks again ! Cheers.
- millercommamatt
- Posts: 832
- Joined: Tue Jul 31, 2018 4:57 pm
Re: AHT20 I2C interfacing (MATLAB PROJECT)
You're welcome. I wasn't able to test this as I don't have matching hardware, but I know Matlab very well so come back if you have issues.
- Dass_Sid
- Posts: 5
- Joined: Wed Apr 21, 2021 2:08 pm
Re: AHT20 I2C interfacing (MATLAB PROJECT)
Thanks Matt. I am facing this error which I was facing earlier when I had tried a different method for bitshifting.millercommamatt wrote:You're welcome. I wasn't able to test this as I don't have matching hardware, but I know Matlab very well so come back if you have issues.
"Error using bitand
Inputs must be signed or unsigned integers of the same class or scalar doubles. "
Even tho I think everything in your implementation share the same class.
- millercommamatt
- Posts: 832
- Joined: Tue Jul 31, 2018 4:57 pm
Re: AHT20 I2C interfacing (MATLAB PROJECT)
Change
to
Code: Select all
raw_temperature = bitshift(bitand(uint32(raw_sensor_data (4)), 0xF),16) + bitshift(uint32(raw_sensor_data (5)),8) + uint32(raw_sensor_data (6));
Code: Select all
raw_temperature = bitshift(bitand(uint32(raw_sensor_data (4)), uint32(0xF)),16) + bitshift(uint32(raw_sensor_data (5)),8) + uint32(raw_sensor_data (6));
- Dass_Sid
- Posts: 5
- Joined: Wed Apr 21, 2021 2:08 pm
Re: AHT20 I2C interfacing (MATLAB PROJECT)
Thank you so much, Matt. It had some minor issues regarding "being double" and stuff but I was able to solve it. Thank you very much for the guidance. I hope I might be able to help someone else like you did.
Please be positive and constructive with your questions and comments.