๐Ÿค–Have you ever tried Chat.M5Stack.com before asking??๐Ÿ˜Ž
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

    M5Stack and I2C distance sensor ks103

    Scheduled Pinned Locked Moved Cores
    2 Posts 1 Posters 5.5k Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C Offline
      cepics
      last edited by

      Hi all, I'm trying to work with This I2C sensor and with the followin sketch:

      #include <Wire.h>
      #define KS103ADD  0x74
      word distance = 0;
      void setup()
      {
      //    Wire.begin();
        Wire.begin(22, 21);//M5STACK: SCL,SDA giallo,bianco
        Serial.begin(115200);
        Wire.beginTransmission(KS103ADD);
        Wire.write(byte(0x02));
        Wire.write(0x72);   // ๅ‘้€้™ๅ™ชๆŒ‡ไปค
        Wire.endTransmission();
        delay(1000);
      }
      void loop()
      {
        if (Serial.read() == 's') {
          while (Serial.read() != 'o') {
            KS103_read();
            Serial.print(distance);
            Serial.print("mm");
            Serial.println();
            delay(250);
          }
        }
        delay(1000);
      }
      
      word KS103_read() {
        Wire.beginTransmission(KS103ADD);
        Wire.write(byte(0x02));
        Wire.write(0xbc);     //Range is set to 10m with temperature compensation
        Wire.endTransmission();
        delay(1);
        Wire.beginTransmission(KS103ADD);
        Wire.write(byte(0x02));
        Wire.endTransmission();
        Wire.requestFrom(KS103ADD, 2);
        if (2 <= Wire.available())
        {
          distance = Wire.read();
          distance =  distance << 8;
          distance |= Wire.read();
        }
      }
      
      

      I have this output:

      0mm
      0mm
      0mm
      0mm
      0mm
      0mm
      0mm
      0mm
      0mm
      0mm
      0mm
      
      

      My question is:
      are they present the pull-up resistors in the M5Stack (black 2018.3) I2C section?

      1 Reply Last reply Reply Quote 0
      • C Offline
        cepics
        last edited by

        now it works:

        /*
           http://www.dauxi.com/KS10X-V110_EN.pdf
        
           11101000
           https://www.arduino.cc/en/Tutorial/SFRRangerReader
        
        
        */
        #include <M5Stack.h>
        #include <Wire.h>
        
        #define KS103ADD  0x74 // questo รจ quello che da lo scanner IIC
        //#define KS103ADD  0xE8 // questo รจ quello che da all'inizio il led verde 11101000    back green led Short flash twice will be โ€œ1โ€, slow flash once will be โ€œ0โ€
        word dist = 0;
        void setup()
        {
          Wire.begin();
        //    Wire.begin(22, 21);//M5STACK: 22/SCL/GIALLO ,21/SDA/BIANCO
          Serial.begin(115200);
          //inizio comando
          Wire.beginTransmission(KS103ADD);
          Wire.write(byte(0x02));
          Wire.write(0x72);   // Invia istruzioni per la riduzione del rumore
          Wire.endTransmission();
          // fine comando
          delay(2000);
        }
        void loop()
        {
          // if (Serial.read() == 's') {
          //  while (Serial.read() != 'o') {
          KS103_read();
          Serial.print(dist/10);
          Serial.print(" cm");
          Serial.println();
          delay(250);
          //}
          //}
          // delay(1000);
        }
        
        void KS103_read() {
          // step 1: instruct sensor to read echoes
          Wire.beginTransmission(KS103ADD);  // transmit to device
          // the address specified in the datasheet is (0xE8)
          // but i2c adressing uses the high 7 bits so it's  (0x74)
          Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
          Wire.write(0xbc);     //Range is set to 10m with temperature compensation
          Wire.endTransmission();
        
          // step 2: wait for readings to happen
          //  delay(1);
          delay(70);   // datasheet suggests at least 65 milliseconds
        
          // step 3: instruct sensor to return a particular echo reading
          Wire.beginTransmission(KS103ADD);  // transmit to device 
          Wire.write(byte(0x02));  // sets register pointer to echo #1 register (0x02)
          Wire.endTransmission();  // stop transmitting
        
          // step 4: request reading from sensor
          Wire.requestFrom(KS103ADD, 2);  // request 2 bytes from slave device
        
          // step 5: receive reading from sensor
          if (2 <= Wire.available()) // if two bytes were received
          {
            dist = Wire.read(); // receive high byte (overwrites previous reading)
            dist =  dist << 8;  // shift high byte to be high 8 bits
            dist |= Wire.read(); // receive low byte as lower 8 bits
          }
        }
        
        1 Reply Last reply Reply Quote 0

        Hello! It looks like you're interested in this conversation, but you don't have an account yet.

        Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

        With your input, this post could be even better ๐Ÿ’—

        Register Login
        • First post
          Last post