# Johnny Five, receiving input from the device

> Learn how to read input from a sensor with Johnny Five and Node.js, wiring a water level sensor to an analog pin and reacting to values with within().

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2021-04-30 | Topics: [JavaScript](https://flaviocopes.com/tags/js/) | Canonical: https://flaviocopes.com/johnny-five-input/

> This post is part of the Johnny Five series. [See the first post here](https://flaviocopes.com/johnny-five/).

In this post I want to get information from an electronic device using Johnny Five.

In particular, I want to use a water level sensor. This will tell me if I got enough coffee or if I'm running out of it, and I need to re-fill the cup in order to be a working programmer.

This is the sensor:

![Red water level sensor module with gold sensing strips and three-pin connector](https://flaviocopes.com/images/johnny-five-input/IMG_9082.jpg)

![Back view of water level sensor showing pin labels - S, +, and - connections](https://flaviocopes.com/images/johnny-five-input/IMG_9083.jpg)

We're going to wire a little circuit to get this data, and we're going to use Johnny Five to get this data into our [Node.js](https://flaviocopes.com/nodejs/) app.

The sensor has 3 pins. One is GND (0V), one is VCC (5V) and the other is the analog data output.

Add the pin marked as `-` to GND, `+` to 5V, and connect `S` to the analog pin `A0`.

![Close-up view of water level sensor with wires connected to its three pins](https://flaviocopes.com/images/johnny-five-input/IMG_9084.jpg)

Here's the circuit:

![Complete Arduino Uno setup with red water level sensor connected via breadboard and jumper wires](https://flaviocopes.com/images/johnny-five-input/IMG_9086.jpg)

Now let's create a `sensor.js` file with this content:

```js
const { Board, Sensor } = require("johnny-five")
const board = new Board()

board.on("ready", () => {
  const sensor = new Sensor("A0")

  sensor.on("change", function () {
    console.log(this.value)
  })
})
```

Whenever the data coming in through the sensor changes, we'll see it being printed to the console:

![Terminal output showing Johnny Five sensor reading numeric values from 0 to 136](https://flaviocopes.com/images/johnny-five-input/Screenshot_2020-03-26_at_17.12.26.png)

I used the `on()` method on the `sensor` object to watch all changes.

[All the methods are detailed here](http://johnny-five.io/api/sensor), but I'm in particular interested in the `within()` method, that lets me define a callback that's fired when the value is in a particular range:

```js
const { Board, Sensor } = require("johnny-five")
const board = new Board()

board.on("ready", () => {
  const sensor = new Sensor("A0")

  sensor.within([0, 70], function () {
    console.log("Refill your coffee!!")
  })
})
```

If I start running out of coffee, the program will print "Refill your coffee!!" a lot of times, because the value keeps changing while the sensor gets drier.

![Terminal output repeatedly showing Refill your coffee!! messages triggered by low sensor values](https://flaviocopes.com/images/johnny-five-input/Screenshot_2020-03-26_at_17.20.44.png)

![Water level sensor partially submerged in coffee cup with LED illuminated indicating active state](https://flaviocopes.com/images/johnny-five-input/IMG_9087.jpg)

So, let's create an `outOfCoffee` variable we can use to debounce the data gathering.

We also declare that under 70 we are out of coffee, and above 150 we have enough:

```js
const { Board, Sensor } = require("johnny-five")
const board = new Board()

board.on("ready", () => {
  const sensor = new Sensor("A0")
  let outOfCoffee = false

  sensor.within([0, 70], () => {
    if (!outOfCoffee) {
      outOfCoffee = true
      console.log("Refill your coffee!!")
    }
  })

  sensor.within([150, 500], () => {
    if (outOfCoffee) {
      outOfCoffee = false
      console.log("Ok, you can go on programming!!")
    }
  })
})
```

That's it, now if I try moving the sensor in/out the cup of coffee, I get some more useful warnings:

![Water level sensor fully immersed in coffee cup showing coffee level detection in action](https://flaviocopes.com/images/johnny-five-input/IMG_9088.jpg)

![Terminal output showing alternating refill and OK programming messages based on coffee levels](https://flaviocopes.com/images/johnny-five-input/Screenshot_2020-03-26_at_17.18.42.png)
