Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Hardware
  3. IR distance sensor usage?

IR distance sensor usage?

Scheduled Pinned Locked Moved Hardware
proximityinfrareddistance
4 Posts 2 Posters 3.1k Views 1 Watching
  • 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.
  • hetbeestH Offline
    hetbeestH Offline
    hetbeest
    wrote on last edited by
    #1

    Hi all,

    I'm pretty new to mysensors, although I read a lot. I bought an IR distance sensor, the Sharp GP2Y0A21YK0F GP2Y0A21 10~80cm Infrared Proximity Distance Sensor, but I don't know how to connect it? Anyone any idea? Maybe also an example sketch?
    Thanks,

    Evert

    1 Reply Last reply
    0
    • hekH Online
      hekH Online
      hek
      Admin
      wrote on last edited by
      #2

      @hetbeest

      You can find a library and examples here.
      https://github.com/jeroendoggen/Arduino-GP2Y0A21YK-library/tree/master/DistanceGP2Y0A21YK

      If/when you have adopted it to MySensors feel free to post it here.

      1 Reply Last reply
      0
      • hetbeestH Offline
        hetbeestH Offline
        hetbeest
        wrote on last edited by
        #3

        Thanks. I'll have a look and surely will post my version.

        1 Reply Last reply
        0
        • hetbeestH Offline
          hetbeestH Offline
          hetbeest
          wrote on last edited by
          #4

          I did some quick coding, combining it with a temperature sensor. It did work for a couple of hours, after that my 9V battery was drained so far that I didn't get any data send from the sensors anymore. Quite likely the IR sensor uses a lot of power while it keeps transmitting IR. Anyhow, my quick and dirty code is:

          // Example sketch showing how to send in OneWire temperature readings
          #include <MySensor.h>
          #include <SPI.h>
          #include <DallasTemperature.h>
          #include <OneWire.h>
          #include <DistanceGP2Y0A21YK.h>

          #define ONE_WIRE_BUS 3 // Pin where dallase sensor is connected
          #define MAX_ATTACHED_DS18B20 16
          #define CHILD_ID_DISTANCE 25
          DistanceGP2Y0A21YK Dist;
          unsigned long SLEEP_TIME = 500; // Sleep time between reads (in milliseconds)
          OneWire oneWire(ONE_WIRE_BUS);
          DallasTemperature sensors(&oneWire);
          MySensor gw;
          float lastTemperature[MAX_ATTACHED_DS18B20];
          int numSensors=0;
          boolean receivedConfig = false;
          boolean metric = true;
          int distance;
          int lastDistance=0;
          int loopCounter=0;
          int loopsBetweenTemp = 60;

          // Initialize temperature message
          MyMessage msg(0,V_TEMP);
          MyMessage msgdist(CHILD_ID_DISTANCE,V_DISTANCE);

          void setup()
          {
          // Startup OneWire
          sensors.begin();
          Dist.begin(0);

          // Startup and initialize MySensors library. Set callback for incoming messages.
          gw.begin();

          // Send the sketch version information to the gateway and Controller
          gw.sendSketchInfo("Temperature and Distance Sensor", "1.0");
          gw.present(CHILD_ID_DISTANCE,S_DISTANCE);
          // Fetch the number of attached temperature sensors
          numSensors = sensors.getDeviceCount();

          // Present all sensors to controller
          for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
          gw.present(i, S_TEMP);
          }
          }

          void loop()
          {
          // Process incoming messages (like config from server)
          gw.process();

          distance = Dist.getDistanceCentimeter();
          if (distance != lastDistance) {
          lastDistance = distance;
          gw.send(msgdist.set(distance));
          }

          if (loopCounter == 0) {
          // Fetch temperatures from Dallas sensors
          sensors.requestTemperatures();

          // Read temperatures and send them to controller 
          for (int i=0; i<numSensors && i<MAX_ATTACHED_DS18B20; i++) {
          
            // Fetch and round temperature to one decimal
            float temperature = static_cast<float>(static_cast<int>((gw.getConfig().isMetric?sensors.getTempCByIndex(i):sensors.getTempFByIndex(i)) * 10.)) / 10.;
          
            // Only send data if temperature has changed and no error
            if (lastTemperature[i] != temperature && temperature != -127.00) {
          
              // Send in the new temperature
              gw.send(msg.setSensor(i).set(temperature,1));
              lastTemperature[i]=temperature;
            }
          }
          

          }
          if (++loopCounter >= loopsBetweenTemp) loopCounter = 0;
          gw.sleep(SLEEP_TIME);
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          15

          Online

          11.7k

          Users

          11.2k

          Topics

          113.0k

          Posts


          Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • MySensors
          • OpenHardware.io
          • Categories
          • Recent
          • Tags
          • Popular