Which bus to use? N2+ GPIO Extension Device with I2C

Post Reply
Nasarhon
Posts: 7
Joined: Fri Dec 03, 2021 9:43 pm
languages_spoken: english, german, hungarian
ODROIDs: ODROID N2+ 4GB
Has thanked: 2 times
Been thanked: 0
Contact:

Which bus to use? N2+ GPIO Extension Device with I2C

Post by Nasarhon »

Hi everyone,

I am testing my N2+ with Ubuntu 20.04 and with a Raspberry Sensor HAT https://www.waveshare.com/wiki/Sense_HAT_(B)

I have the device connected and it shows up with

Code: Select all

i2cdetect -y 0
The example programs are running but they are not getting any data.

Should the sensor device be connected to I2C-1 instead of 0? If yes how can I do that?

Thanks in advance!

User avatar
mctom
Posts: 1016
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 104 times
Been thanked: 125 times
Contact:

Re: Which bus to use? N2+ GPIO Extension Device with I2C

Post by mctom »

Hi,

The example software may not run properly, because they ask you to install a Broadcom-specific library, so I assume the example code uses it.
Before you run examples, you should install related libraries.
BCM2835 library:
Instead, you probably should inspect example code and rewrite it using Odroid-specific libraries.

Querying I2C devices should be easy, probably may be done even in bash.
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

User avatar
mctom
Posts: 1016
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 104 times
Been thanked: 125 times
Contact:

Re: Which bus to use? N2+ GPIO Extension Device with I2C

Post by mctom »

Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

Nasarhon
Posts: 7
Joined: Fri Dec 03, 2021 9:43 pm
languages_spoken: english, german, hungarian
ODROIDs: ODROID N2+ 4GB
Has thanked: 2 times
Been thanked: 0
Contact:

Re: Which bus to use? N2+ GPIO Extension Device with I2C

Post by Nasarhon »

mctom wrote:
Tue Dec 07, 2021 10:12 pm
Hi,

The example software may not run properly, because they ask you to install a Broadcom-specific library, so I assume the example code uses it.
Before you run examples, you should install related libraries.
BCM2835 library:
Instead, you probably should inspect example code and rewrite it using Odroid-specific libraries.

Querying I2C devices should be easy, probably may be done even in bash.
Thanks mctom! I will do some examination and reading regarding the example code.

User avatar
mctom
Posts: 1016
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 104 times
Been thanked: 125 times
Contact:

Re: Which bus to use? N2+ GPIO Extension Device with I2C

Post by mctom »

You welcome. Please also post more command responses so there's something more to work on. :)

for example, what does i2cdetect return? Just one device, or more? I think all these sensors should be seen as separate I2C devices.
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

Nasarhon
Posts: 7
Joined: Fri Dec 03, 2021 9:43 pm
languages_spoken: english, german, hungarian
ODROIDs: ODROID N2+ 4GB
Has thanked: 2 times
Been thanked: 0
Contact:

Re: Which bus to use? N2+ GPIO Extension Device with I2C

Post by Nasarhon »

mctom wrote:
Tue Dec 07, 2021 10:27 pm
You welcome. Please also post more command responses so there's something more to work on. :)

for example, what does i2cdetect return? Just one device, or more? I think all these sensors should be seen as separate I2C devices.
Yes, all of them are in the i2c table:

Code: Select all

~$ sudo i2cdetect -y 0

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- 29 -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- 5c -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- --
I ve read the manual one more time and it seems the "related libraries" means any of them below, I did install odroid-wiringpi according to the odroid wiki.

So with the wiringPi examples it should be fine as far as I can tell, and I have a return for this command as well:

Code: Select all

~$ gpio readall
 +-----+-----+---------+------+---+-Model  A-+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 |     |     |    3.3v |      |   |  1 || 2  |   |      | 5v      |     |     |
 |   2 |   8 |   SDA.1 |   IN | 0 |  3 || 4  |   |      | 5v      |     |     |
 |   3 |   9 |   SCL.1 |   IN | 0 |  5 || 6  |   |      | 0v      |     |     |
 |   4 |   7 | GPIO. 7 |   IN | 0 |  7 || 8  | 0 | IN   | TxD     | 15  | 14  |
 |     |     |      0v |      |   |  9 || 10 | 0 | IN   | RxD     | 16  | 15  |
 |  17 |   0 | GPIO. 0 |   IN | 0 | 11 || 12 | 0 | IN   | GPIO. 1 | 1   | 18  |
 |  27 |   2 | GPIO. 2 |   IN | 0 | 13 || 14 |   |      | 0v      |     |     |
 |  22 |   3 | GPIO. 3 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO. 4 | 4   | 23  |
 |     |     |    3.3v |      |   | 17 || 18 | 0 | IN   | GPIO. 5 | 5   | 24  |
 |  10 |  12 |    MOSI |   IN | 0 | 19 || 20 |   |      | 0v      |     |     |
 |   9 |  13 |    MISO |   IN | 0 | 21 || 22 | 0 | IN   | GPIO. 6 | 6   | 25  |
 |  11 |  14 |    SCLK |   IN | 0 | 23 || 24 | 0 | IN   | CE0     | 10  | 8   |
 |     |     |      0v |      |   | 25 || 26 | 0 | IN   | CE1     | 11  | 7   |
 +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
 | BCM | wPi |   Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 +-----+-----+---------+------+---+-Model  A-+---+------+---------+-----+-----+
I am doing my tests with the SHTC3 sensor for temperature and humidity and the address for that is correct, according to the example code it is "0x70" as it is in the i2c table.

I am not really sure where I am missing something.

Nasarhon
Posts: 7
Joined: Fri Dec 03, 2021 9:43 pm
languages_spoken: english, german, hungarian
ODROIDs: ODROID N2+ 4GB
Has thanked: 2 times
Been thanked: 0
Contact:

Re: Which bus to use? N2+ GPIO Extension Device with I2C

Post by Nasarhon »

Oh and this is the example code I am running:

Code: Select all

#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <stdio.h>
#include <math.h>
#include"SHTC3.h"
#include <unistd.h>
float TH_Value,RH_Value;
char checksum;
int fd;
char SDA = 8;
char SCL = 9;
char SHTC3_CheckCrc(char data[],unsigned char len,unsigned char checksum)
{
  unsigned char bit;        // bit mask
  unsigned char crc = 0xFF; // calculated checksum
  unsigned char byteCtr;    // byte counter
  // calculates 8-Bit checksum with given polynomial
  for(byteCtr = 0; byteCtr < len; byteCtr++) {
    crc ^= (data[byteCtr]);
    for(bit = 8; bit > 0; --bit) {
      if(crc & 0x80) {
        crc = (crc << 1) ^ CRC_POLYNOMIAL;
      } else {
        crc = (crc << 1);
      }
    }
  }

  // verify checksum
  if(crc != checksum) {                 
    return 1;                       //Error
  } else {
    return 0;                       //No error
  }       
}
void SHTC3_WriteCommand(unsigned short cmd)
{   
    char buf[] = { (cmd>>8) ,cmd};
    wiringPiI2CWriteReg8(fd,buf[0],buf[1]);          
                                                 //1:error 0:No error
}
void SHTC3_WAKEUP()
{     
    SHTC3_WriteCommand(SHTC3_WakeUp);                  // write wake_up command  
    delayMicroseconds(300);                          //Delay 300us
      
}
void SHTC3_SLEEP()
{    
 //   bcm2835_i2c_begin();
    SHTC3_WriteCommand(SHTC3_Sleep);                        // Write sleep command
      
}

void SHTC_SOFT_RESET()
{   
    SHTC3_WriteCommand(SHTC3_Software_RES);                 // Write reset command
    delayMicroseconds(300);                                 //Delay 300us
     
}

void SHTC3_Read_DATA()
{   
    unsigned short TH_DATA,RH_DATA;
    char buf[3];
   SHTC3_WriteCommand(SHTC3_NM_CD_ReadTH);                 //Read temperature first,clock streching disabled (polling)
    delay(20);
    read(fd, buf, 3);

   checksum=buf[2];
   if(!SHTC3_CheckCrc(buf,2,checksum))
        TH_DATA=(buf[0]<<8|buf[1]);
    
    SHTC3_WriteCommand(SHTC3_NM_CD_ReadRH);                 //Read temperature first,clock streching disabled (polling)
    delay(20);
    read(fd, buf, 3);

    checksum=buf[2];
    if(!SHTC3_CheckCrc(buf,2,checksum))
        RH_DATA=(buf[0]<<8|buf[1]);
    
    TH_Value=175 * (float)TH_DATA / 65536.0f - 45.0f;       //Calculate temperature value
    RH_Value=100 * (float)RH_DATA / 65536.0f;              //Calculate humidity value  
    
}

int main()
{   
    printf("\n SHTC3 Sensor Test Program ...\n");
    if (wiringPiSetup() < 0) return 1;
    fd=wiringPiI2CSetup(SHTC3_I2C_ADDRESS);
    SHTC_SOFT_RESET();
    while (1)
    {
        SHTC3_Read_DATA();
        SHTC3_SLEEP();
        SHTC3_WAKEUP();

	    printf("Temperature = %6.2f°C , Humidity = %6.2f%% \r\n", TH_Value, RH_Value);
    }
    return 0;
}
And there are the commands and addresses:

Code: Select all

#ifndef _SHTC3_H
#define _SHTC3_H

//i2c address
#define SHTC3_I2C_ADDRESS	    	0x70

//Commands
#define SHTC3_WakeUp                0x3517
#define SHTC3_Sleep                 0xB098
#define SHTC3_NM_CE_ReadTH          0x7CA2
#define SHTC3_NM_CE_ReadRH          0x5C24
#define SHTC3_NM_CD_ReadTH          0x7866
#define SHTC3_NM_CD_ReadRH          0x58E0
#define SHTC3_LM_CE_ReadTH          0x6458
#define SHTC3_LM_CE_ReadRH          0x44DE
#define SHTC3_LM_CD_ReadTH          0x609C
#define SHTC3_LM_CD_ReadRH          0x401A
#define SHTC3_Software_RES          0x401A
#define SHTC3_ID               0xEFC8

#define CRC_POLYNOMIAL              0x131 // P(x) = x^8 + x^5 + x^4 + 1 = 100110001

#endif

User avatar
tobetter
Posts: 8933
Joined: Mon Feb 25, 2013 10:55 am
languages_spoken: Korean, English
ODROIDs: Many
Location: Paju, South Korea
Has thanked: 521 times
Been thanked: 1433 times
Contact:

Re: Which bus to use? N2+ GPIO Extension Device with I2C

Post by tobetter »

@Nasarhon, are you going to use Linux instead of Android? Because you have posted the project for Android earlier.

Nasarhon
Posts: 7
Joined: Fri Dec 03, 2021 9:43 pm
languages_spoken: english, german, hungarian
ODROIDs: ODROID N2+ 4GB
Has thanked: 2 times
Been thanked: 0
Contact:

Re: Which bus to use? N2+ GPIO Extension Device with I2C

Post by Nasarhon »

tobetter wrote:
Tue Dec 07, 2021 11:03 pm
@Nasarhon, are you going to use Linux instead of Android? Because you have posted the project for Android earlier.
Hi! Yes I am exploring my possibilities. And also I am hoping to understand more on the subject (as I am a new to this) if I minimize the unknowns :D Because everything works fine if I use my Raspberry 3B+ on Linux and this Sensor HAT, now I am trying to make it work on ODROID and Linux, and eventually ODROID + Android. I have to get some insight to how the hardware software connections work. I am a programmer and a network engineer, so I have some head start, but still new territory.

User avatar
mctom
Posts: 1016
Joined: Wed Nov 11, 2020 4:44 am
languages_spoken: english, polish
ODROIDs: N2+, Game Advance, a few XU4
Location: Gdansk, Poland
Has thanked: 104 times
Been thanked: 125 times
Contact:

Re: Which bus to use? N2+ GPIO Extension Device with I2C

Post by mctom »

'gpio readall' output definitely looks like it still thinks it is run on a Raspberry Pi. Note there are "BCM" columns in there, indicating pin numbers on BCM chip that is a SoC of RasPi.

WiringPi is generally a library made specifically for Raspberry Pi, and the Odroid variant is not a drop-in replacement that just works. If you really want to use WiringPi, expect many things to not work like on a device it was made for.
If you want to go WiringPi way, here's another chapter of Wiki for you to digest:
https://wiki.odroid.com/odroid-n2/appli ... o/wiringpi
(There is also an output of 'gpio readall' that you should get instead).

And you really don't need WiringPi to read a number via I2C. I think you may google something like "read /dev/i2c" or something. I've never done that, but I imagine this is the purpose of these interfaces to begin with.
These users thanked the author mctom for the post:
Nasarhon (Thu Dec 09, 2021 11:09 pm)
Punk ain't no religious cult, punk means thinking for yourself!

Maintainer of PiStackMon

Post Reply

Return to “Hardware and peripherals”

Who is online

Users browsing this forum: tobetter, wallyz21 and 1 guest