Arduino input

Different kinds of inputs for Arduino

Use the list below to get an overview of the different kinds of inputs for Arduino. The list is only some of the more commonly used. There are endless amount of sensors out there (gas, CO2, radiation, movement etc) that you can use. You can look them up by searching for e.g: "CO2 sensor arduino diagram" on google images. You will then get the diagram and the link often provides you with the code and the parts you need to order.

libraries in arduino

Libraries in Arduino are extra “packs” that you can load to open up more functions to work with, e.g. a library connected to a specific hardware element. Some elements require specific libraries in order for the setup to work.

To see what libraries you have and to add extra ones, go to - Sketch - Include library - Manage libraries -. You can search for libraries, and they will show up with name and description, and you can then install it from here.

If you need a special library which can’t be found here, sometimes they can be found as .ZIP files online directly from the producer or from open source networks. Once downloaded, you can go to - Sketch - Include library - Add .ZIP library - and import the file.

Once your library is downloaded, go to - Sketch - Include library - and chose the library you need, and it will appear with #include <NameOfLibrary.h> (and possibly a selection of other necessary elements to run the library) in the very top of the sketch, and it is ready to use!





Read more about libraries in Arduino here.

inputs:

Push button

A push button is a simple way to create interaction. It can be used to create different outputs depending on the length of the push, the amount of pushes, etc.

Attach one wire from the button to a digital pin (in the example we have used digital pin 3), and the other to ground. To test the circuit, use the following code:

void setup() {

pinMode(3, INPUT_PULLUP);

Serial.begin(9600);

}

void loop() {

boolean btnPressed = !digitalRead(3);

if(btnPressed == true) {

Serial.println("button has been pressed.");

}

}




Booleans (also called bool) are a type of variable. Read more about it here.

Potentiometer

A potentiometer is a mechanical device providing a resistance, which can be read as an analogue value using Arduino. When the knob of the potentiometer is turned, the amount of resistance varies, creating increasing and decreasing values. This can be used the same way we use knobs on e.g. speakers to turn up or down the volume.

The three wires of the potentiometer go to ground, 5v and one of the analogue pins (in this example A0).

Use this code to read the value in the serial monitor:

int potPin = A0;


void setup() {

Serial.begin(9600);

}

void loop() {

int sensorValue = analogRead(potPin);

Serial.println(sensorValue);

delay(1);

}

Integers (int) are a type of variables. Read more about it here.

IR distance: Sensing the distance from an object

The infrared distance sensor uses infrared pulses to measure the distance from the sensor and to whatever is in front of it. This can be used in setups where a certain output is comes from being closer or further away from the sensor. (Note that the sensors will usually look like the ones from the picture, and not from the diagram).

Connect power to 5v, ground to ground and output to analog pin 0.

In order to test if the sensor is working, use the following code:

int irPin = A0;


void setup() {

Serial.begin(9600);

}

void loop() {

Serial.println(analogRead(irPin));

}


Analog read and serial print are functions that allow you to view the input directly in the serial monitor. Read more about it here.

Microphone: Sensing frequencies and volume

The microphone sound sensor measures sound intensity and gives values for you to read on the serial monitor or convert to an output of your choice.

Some microphones have both a digital and an analogue output; read up on your specific microphone for the correct wiring. Otherwise connect ground to ground, power to 5v and the analogue output to an analogue pin (in this example A0). Most microphones will also have a built-in potentiometer with which you can adjust the sensitivity of the sensor.

Use this code to read the values of the microphone, and test it by clapping or making different sounds:

int soundPin = A0;

int sensorValue = 0;

void setup() {

Serial.begin (9600);

}

void loop() {

sensorValue = analogRead(soundPin);

Serial.println(sensorValue, DEC);

delay(1000);

}

Piezo - as a knock sensor

The Piezo is a sensor which produces AC voltage when in movement. It can be used both as a source of input and output. It creates input when moved, flickered or shaken (could be by the knock of a hand, by a sound wave, etc.) and can be used for interactions where differing intensities or types of movement gives different outputs.

Connect ground to ground and power to analog pin 0, using a 1 M ohm resistor (breadboard is optional).

Use the following code to read the values (ADC) and convert into voltage:

const int PIEZO_PIN = A0;

void setup() {

Serial.begin(9600);

}

void loop() {

int piezoADC = analogRead(PIEZO_PIN);

float piezoV = piezoADC / 1023.0 * 5.0;

Serial.println(piezoV);

}

You have already been presented to the int and boolean variables. Read here for more information about the float variable, or here for the const variable.

MPU: Sensing movement and direction

The MPU is a version of an IMU (inertia measurement unit) sensor, which can measure three-dimensional movement and positions. The MPU is a combination of an accelerometer (measures gravitational acceleration) and a gyroscope (measures rotational velocity).

Install the library “MPU6050” by Adafruit – it might ask you to install a series of other libraries in order for this to run.

Go to – File – Examples – Adafruit MPU6050 – and chose the example called “plotter” in order to read the values from the accelerometer in the serial plotter.



The plotter-example looks like this:

#include <Adafruit_MPU6050.h>

#include <Adafruit_Sensor.h>

#include <Wire.h>

Adafruit_MPU6050 mpu;

void setup(void) {

Serial.begin(115200);

while (!Serial) {

delay(10);

}

if (!mpu.begin()) {

Serial.println("Failed to find MPU6050 chip");

while (1) {

delay(10);

}

}

mpu.setAccelerometerRange(MPU6050_RANGE_16_G);

mpu.setGyroRange(MPU6050_RANGE_250_DEG);

mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);

Serial.println("");

delay(100);

}

void loop() {

sensors_event_t a, g, temp;

mpu.getEvent(&a, &g, &temp);

Serial.print(a.acceleration.x);

Serial.print(",");

Serial.print(a.acceleration.y);

Serial.print(",");

Serial.print(a.acceleration.z);

Serial.print(", ");

Serial.print(g.gyro.x);

Serial.print(",");

Serial.print(g.gyro.y);

Serial.print(",");

Serial.print(g.gyro.z);

Serial.println("");

delay(10);

}

Touch sensor (capacitance)

Capacitive touch sensing can be created with something as simple as a wire. It is based on an instrument invented by Léon Theremin. The instrument detects the capacitance between an antenna and the human body. The capacitance then affects the frequency of a sine wave. Since the distance between the human body and the antenna is relative to the capacitance, it creates different values depending on the distance between them. It is not necessary to touch the antenna and, therefore, it serves as an efficient way of sensing the aura between the object and the body. Even though it traditionally consisted of an antenna, it could just as well be anything else that is conductive. This includes, but is not limited to, plants, human bodies, water, metal objects, fruits, vegetables, etc.

Connect a wire to analog zero. Include the library ”ADCTouch” by Alexander Pruss, and use the following code to view the values in the serial monitor:

Connect one wire to analog zero and the other to ground.

Include the library ”ADCTouch” by Alexander Pruss, and use the following code to view the values in the serial monitor:

#include <ADCTouchSensor.h>

ADCTouchSensor touch0 = ADCTouchSensor(A0, -1);

void setup() {

Serial.begin(9600);

touch0.begin();

}

void loop() {

float value0 = touch0.read();

Serial.println(value0);

}











If you want to learn more about the ADCTouch library, look here.

Capacitance: Sensing moistore level

You can also use capacitive sensing to measure e.g. soil moisture in plants. Since water conducts electricity better than soil, when there is more water in the soil, the capacitance increases. You can use something as simple as nails (or something else conductive) soldered to the wires, and by the same principles and basic code as in the one point or two point capacitive sensing (try both and see which works the best) just translate the capacitance values into moisture level by testing different amounts of water in the soil.


Extra: create an output (e.g. a screen as seen in the photo) to indicate the moisture level.

Two point connection sensing

By using two “antennas” as sender and receiver, you can detect touch in between the two points.

Connect one sensor to A0 and the other to A1, and use the following code:

void setup() {

}

void loop() {

float value = touchRead(A0, A1);

}

int touchRead(int pin1, int pin2) {

int sum=0;

for(int i=0; i < 10; i++)

{

pinMode(pin1, OUTPUT);

digitalWrite(pin1,HIGH);

delayMicroseconds(10);

int high = analogRead(pin2);

digitalWrite(pin1,LOW);

delayMicroseconds(10);

int low = analogRead(pin2);

pinMode(pin1, INPUT);

digitalWrite(pin1,LOW);

sum = sum +high-low;

}

return (int)((float)sum/4.0f);

}


Extra: connect an output (e.g. piezo) to create an interaction where something happens, when an object is detected between the two points.

Using two analogue pins you can sense the connection between two metal poles. E.g. you can make installations where people needs to hold hands to trigger something.

Pulse: Sensing the human heartbeat.

The pulse sensor measures your pulse through your fingertip. In this example we will be using the MAX30100 Pulse Oximeter Sensor, but there are other types of pulse sensors also compatible with Arduino.

The pulse sensor measures your pulse through your fingertip. In this example we will be using the MAX30100 Pulse Oximeter Sensor, but there are other types of pulse sensors also compatible with Arduino.

Connect VIN (voltage in) to 5v, ground to ground, SCL to an analog pin (here A5) and SDA to analog pin (A4).

Download the library MAX30100lib by OXullo Intersescans. In examples, choose ”MAX30100lib_Tester” to try out the sensor and view data in the serial monitor. The code looks like this:

The code

#include <Wire.h>

#include "MAX30100.h"

MAX30100 sensor;

void setup()

{

Serial.begin(115200);

Serial.print("Initializing MAX30100..");

if (!sensor.begin()) {

Serial.print("FAILED: ");

uint8_t partId = sensor.getPartId();

if (partId == 0xff) {

Serial.println("I2C error");

} else {

Serial.print("wrong part ID 0x");

Serial.print(partId, HEX);

Serial.print(" (expected: 0x");

Serial.println(EXPECTED_PART_ID, HEX);

}

for(;;);

} else {

Serial.println("Success");

}

Serial.print("Enabling HR/SPO2 mode..");

sensor.setMode(MAX30100_MODE_SPO2_HR);

Serial.println("done.");

Serial.print("Configuring LEDs biases to 50mA..");

sensor.setLedsCurrent(MAX30100_LED_CURR_50MA, MAX30100_LED_CURR_50MA);

Serial.println("done.");

delay(1000);

Serial.print("Lowering the current to 7.6mA..");

sensor.setLedsCurrent(MAX30100_LED_CURR_7_6MA, MAX30100_LED_CURR_7_6MA);

Serial.println("done.");

delay(1000);

Serial.print("Shutting down..");

sensor.shutdown();

Serial.println("done.");

delay(1000);

Serial.print("Resuming normal operation..");

sensor.resume();

delay(500);

Serial.println("done.");

uint32_t tsTempSampStart = millis();

Serial.print("Sampling die temperature..");

sensor.startTemperatureSampling();

while(!sensor.isTemperatureReady()) {

if (millis() - tsTempSampStart > 1000) {

Serial.println("ERROR: timeout");

// Stop here

for(;;);

}

}

float temperature = sensor.retrieveTemperature();

Serial.print("done, temp=");

Serial.print(temperature);

Serial.println("C");

if (temperature < 5) {

Serial.println("WARNING: Temperature probe reported an odd value");

} else {

Serial.println("All test pass.");

}

Serial.println();

Serial.println("Press any key to go into sampling loop mode");

while (!Serial.available());

sensor.resetFifo();

}

void loop()

{

uint16_t ir, red;

sensor.update();

while (sensor.getRawValues(&ir, &red)) {

Serial.print("IR=");

Serial.print(ir);

Serial.print(" RED=");

Serial.println(red);

}

}