# Human Presence Sensor Breakout - AK9753 Calibration?

**URL:** <https://community.sparkfun.com/t/human-presence-sensor-breakout-ak9753-calibration/43855>\
**Category:** Distance\
**Created:** [April 27, 2022, 12:03am UTC](https://community.sparkfun.com/t/human-presence-sensor-breakout-ak9753-calibration/43855 "2022-04-27T00:03:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Landtree](https://avatars.discourse-cdn.com/v4/letter/l/c37758/32.png) [@Landtree](https://community.sparkfun.com/u/Landtree)\
**Post date:** [April 27, 2022, 12:03am UTC](https://community.sparkfun.com/t/human-presence-sensor-breakout-ak9753-calibration/43855/1 "2022-04-27T00:03:02Z")

</div>

Hey Everyone,

I am struggling to find a way to get semi-consistent data out of this. I don’t need to be super accurate but I will get swinging values each time I upload new code.

It basically goes something like I boot it up and if it sees me ill get an output of say 100 then it sits on and then I update with the new code and this time when it sees me it’s at -2 even though I try and stand about the same distance away each time.

(I do a 10 average to get the numbers)

for (byte i = 0; i \<= 10; i++)

{

ir1 = movementSensor.getIR1();

movementSensor.refresh();

ir1Last = ir1 + ir1Last;

delay(10);

if (i == 10)

{

ir1dif = ir1 - (ir1Last / 10);

if (inRange(ir1dif, SenseMin, SenseMax) )

{

Serial.println(“in Range”);

lightsOn = true;

}

else

{

Serial.print("out of Range: ");

Serial.println(ir1dif);

lightsOn = false;

}

ir1Last = 0;

ir1dif = 0;

}

}

movementSensor.refresh();

}

I was hoping to create a threshold limit of knowing when someone is in front of it. I even tried at the boot to capture what the number is when you are standing at it 2ft away and then 3ft away so I would know if someone is in front of it roughly in that location.

Long story short, I know it’s affected by ambient factors but are there any implementations I could put into effect to help mitigate this? This will be embedded into a wall sculpture in a gallery and ideally, I wouldn’t have to come in and figure out the calibration for that moment.

Any insight would be great!

Thanks!

---

<div class="post-metadata">

**Author:** ![paulvha](https://avatars.discourse-cdn.com/v4/letter/p/65b543/32.png) [@paulvha](https://community.sparkfun.com/u/paulvha)\
**Post date:** [April 27, 2022, 10:13am UTC](https://community.sparkfun.com/t/human-presence-sensor-breakout-ak9753-calibration/43855/2 "2022-04-27T10:13:48Z")

</div>

Change the code to first check that data is available: if (movementSensor.available()). For your purpose it is better to read all the IR values and act on a change of either of them as each takes a little different angle.

---

<div class="post-metadata">

**Author:** ![Landtree](https://avatars.discourse-cdn.com/v4/letter/l/c37758/32.png) [@Landtree](https://community.sparkfun.com/u/Landtree)\
**Post date:** [April 27, 2022, 4:15pm UTC](https://community.sparkfun.com/t/human-presence-sensor-breakout-ak9753-calibration/43855/3 "2022-04-27T16:15:57Z")

</div>

Okay, I am using the movement sensor.available(). that was just a snippet of a nested if statement. I will try coding something to look for a change!

I am wondering if it would ever flatline? The overall goal is Sculpture activates while someone is in front of it, the longer they stay the more exciting the lights get. So, if they are standing there and say after 30 secs is flat lines? Just something I am wondering if I need to look out for! I will use all 4 sensors when I write this new function. Thanks for the advice!

Here is the full function:

void HumanCheck()

{

if (allowIR == true)

{

if (lightsOn == false)

{

if (movementSensor.available())

{

for (byte i = 0; i \<= 10; i++)

{

ir1 = movementSensor.getIR1();

movementSensor.refresh();

ir1Last = ir1 + ir1Last;

delay(10);

if (i == 10)

{

ir1dif = ir1 - (ir1Last / 10);

if (inRange(ir1dif, SenseMin, SenseMax) )

{

Serial.println(“in Range”);

lightsOn = true;

Serial.println(“Run Warmup”);

}

else

{

Serial.print("out of Range: ");

Serial.println(ir1dif);

lightsOn = false;

}

ir1Last = 0;

ir1dif = 0;

}

}

movementSensor.refresh();

}

}

}

}

---

<div class="post-metadata">

**Author:** ![paulvha](https://avatars.discourse-cdn.com/v4/letter/p/65b543/32.png) [@paulvha](https://community.sparkfun.com/u/paulvha)\
**Post date:** [April 27, 2022, 4:51pm UTC](https://community.sparkfun.com/t/human-presence-sensor-breakout-ak9753-calibration/43855/4 "2022-04-27T16:51:52Z")

</div>

I think you are trying to do too much in a single function. break it down into a number of different functions :

Function A. Read the sensor and store the values.

Function B. Logic to detect whether someone is (still) in front or not.

Function C. controls the light brightness, increases based on the seconds someone is in front or (slowly) reduces if no one.

etc.

Function A could look like :

```auto
void ReadSensor()
{
  uint8_t i = 0;
  // global defined
  ir1Last = ir2Last = ir3Last = ir4Last = 0;
  
  if (allowIR) {

    while (i <= 10) {
      if (movementSensor.available()) {
        ir1Last += movementSensor.getIR1();
        ir2Last += movementSensor.getIR2();
        ir3Last += movementSensor.getIR3();
        ir4Last += movementSensor.getIR4();
        movementSensor.refresh();
        i++;
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Landtree](https://avatars.discourse-cdn.com/v4/letter/l/c37758/32.png) [@Landtree](https://community.sparkfun.com/u/Landtree)\
**Post date:** [April 27, 2022, 6:24pm UTC](https://community.sparkfun.com/t/human-presence-sensor-breakout-ak9753-calibration/43855/5 "2022-04-27T18:24:06Z")

</div>

Switched it to your exampled and this is the ticket! Thank you so much. It is also way more responsive with this method.
