IR distance sensor usage?



  • 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


  • Admin

    @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.



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



  • 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);
    }



Suggested Topics

  • 3
  • 6
  • 7
  • 5
  • 3
  • 87

58
Online

11.4k
Users

11.1k
Topics

112.7k
Posts