Circuit Playground Express IR Proximity sensor

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
raldi
 
Posts: 25
Joined: Fri Mar 03, 2017 3:22 am

Circuit Playground Express IR Proximity sensor

Post by raldi »

The CPE guided tour (https://learn.adafruit.com/adafruit-cir ... uided-tour) mentions that the IR receiver can be used as a crude proximity sensor:
There's also a secret capability, you can read the 'raw' analog value from the receiver LED diode to do basic IR proximity sensing. The direct analog value is available from pin A10
However, when I write a simple loop function to analogRead(A10) and print the result, it only ever varies randomly between 309 and 311, irrespective of whether the sensor is in the dark, in bright light, with an active remote control pointed at it, with my hand in front of it, etc. Nothing I can do will get any other values out of it. Searching these forums for help, I found two existing posts, but neither ever got an answer. One called analogReadResolution(12) in its setup(), so I gave that a try too, but that just changed my range of values to 1240-1246, and again, the values don't seem to be influenced by anything I do.

Has anyone actually ever gotten this to work, and if so, could you share the code?

User avatar
mikeysklar
 
Posts: 13936
Joined: Mon Aug 01, 2016 8:10 pm

Re: Circuit Playground Express IR Proximity sensor

Post by mikeysklar »

I don't think we have a code example for the CPX doing proximity sensing, but I can point you at some straight C code that might help you get it going with Arduino.

https://github.com/family-richards/Circ ... -proximity

Code: Select all

#include <Adafruit_CircuitPlayground.h>

#if !defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS)
#error "Infrared support is only for the Circuit Playground Express, it doesn't work with other boards."
#endif

void setup() {
  //Setup everything
  CircuitPlayground.begin();
  pinMode(CPLAY_IR_EMITTER, OUTPUT);
  digitalWrite(CPLAY_IR_EMITTER, LOW);
  pinMode(A8, INPUT);
  pinMode(13, OUTPUT);
  Serial.begin(19200);
}
int IR;
void on() {
  //Turn all NeoPixels on
  CircuitPlayground.setPixelColor(0, 255, 255, 255);
  CircuitPlayground.setPixelColor(1, 255, 255, 255);
  CircuitPlayground.setPixelColor(2, 255, 255, 255);
  CircuitPlayground.setPixelColor(3, 255, 255, 255);
  CircuitPlayground.setPixelColor(4, 255, 255, 255);
  CircuitPlayground.setPixelColor(5, 255, 255, 255);
  CircuitPlayground.setPixelColor(6, 255, 255, 255);
  CircuitPlayground.setPixelColor(7, 255, 255, 255);
  CircuitPlayground.setPixelColor(8, 255, 255, 255);
  CircuitPlayground.setPixelColor(9, 255, 255, 255);
}
void off() {
  //Turn all NeoPixels off
  CircuitPlayground.setPixelColor(0, 0, 0, 0);
  CircuitPlayground.setPixelColor(1, 0, 0, 0);
  CircuitPlayground.setPixelColor(2, 0, 0, 0);
  CircuitPlayground.setPixelColor(3, 0, 0, 0);
  CircuitPlayground.setPixelColor(4, 0, 0, 0);
  CircuitPlayground.setPixelColor(5, 0, 0, 0);
  CircuitPlayground.setPixelColor(6, 0, 0, 0);
  CircuitPlayground.setPixelColor(7, 0, 0, 0);
  CircuitPlayground.setPixelColor(8, 0, 0, 0);
  CircuitPlayground.setPixelColor(9, 0, 0, 0);
}
void gray(int val) {
  //Turn all NeoPixels to a percent of white
  CircuitPlayground.setPixelColor(0, val, val, val);
  CircuitPlayground.setPixelColor(1, val, val, val);
  CircuitPlayground.setPixelColor(2, val, val, val);
  CircuitPlayground.setPixelColor(3, val, val, val);
  CircuitPlayground.setPixelColor(4, val, val, val);
  CircuitPlayground.setPixelColor(5, val, val, val);
  CircuitPlayground.setPixelColor(6, val, val, val);
  CircuitPlayground.setPixelColor(7, val, val, val);
  CircuitPlayground.setPixelColor(8, val, val, val);
  CircuitPlayground.setPixelColor(9, val, val, val);
}
void mid() {
  //Turn all NeoPixels to gray
  gray(64);
}
bool prev;
bool isOn;
long lastActivation;
void loop() {
  if (CircuitPlayground.slideSwitch()) {
    //Run if mode is proximity
    //Turn off LED to show proximity mode
    digitalWrite(13, LOW);
    //Don't spend too much time with IR LED on
    delay(200);
    //Turn IR LED on
    digitalWrite(CPLAY_IR_EMITTER, HIGH);
    //Let analog val set
    delay(50);
    //Read proximity
    IR = analogRead(A8);
    //Turn off IR LED
    digitalWrite(CPLAY_IR_EMITTER, LOW);
    //Print proximity so you can plot
    Serial.println(map(IR, 930, 970, 0, 100));
    //Make val into a more easy to manage number
    IR = map(IR, 930, 970, 0, 100);
    if (IR > 70) {
      //Close to object
      if (!prev) {
        //Close to object but not when ran last loop
        mid();
        prev = true;
        lastActivation = millis();
      } else {
        //Close to object in last loop and this loop
        on();
        prev = true;
        lastActivation = millis();
      }
    } else {
      //Not close to object
      if (millis() - lastActivation < 10000 && millis() - lastActivation > 9700) {
        //Been close to object in 9700-10000 milliseconds
        mid();
        prev = false;
      } else if (millis() - lastActivation > 10000) {
        //Haven't been close to object within 10 seconds
        off();
        prev = false;
      } else {
        //Fix bug for when you only activate the IR sense in 1 loop so the NeoPixels are at mid bright for the time
        on();
      }
    }
  } else {
    //Manual mode, so show LED and push back lastActivation
    digitalWrite(13, HIGH);
    lastActivation = -50000;
    if (CircuitPlayground.rightButton()) {
      //On button is pressed
      isOn = true;
    } else if (CircuitPlayground.leftButton()) {
      //Off button is pressed and on button isn't pressed
      isOn = false;
    }
    //Manage NeoPixels
    if (isOn) {
      on();
    } else {
      off();
    }
  }
}

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”