Skip to content

13. Input Devices

This week we were tasked with probing an input device’s analog levels and digital signals. We decided to use the Grove board and kit for the Arduino Uno again for the exercise.

Touch Sensor - Digital Signal

The grove touch sensors example code was the same as the Grove Button. This was designed to tuen on a LED when it detected an input.

I modified the code so it just outputed to the serial moniter the number of touches it has detected. alt text

const int buttonPin = 2;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status
int num_touch = 0;


void setup() {
    // initialize the LED pin as an output:
    // initialize the pushbutton pin as an input:
    pinMode(buttonPin, INPUT);
    Serial.begin(9600);
}

void loop(){
    // read the state of the pushbutton value:
    buttonState = digitalRead(buttonPin);

    // check if the pushbutton is pressed.
    // if it is, the buttonState is HIGH:
    if (buttonState == HIGH) {
        // turn LED on:
        num_touch ++;
        Serial.print("Touch:");
        Serial.println(num_touch);
        delay(100);
    }
    else {
    }
}

Video of the touch counter ticking up:

Video of the digital signal being detected by the Saleae.

Temerature Sensor - Analogue Signal

I then used the temperature sensor as the analogue input signal. Temperature Sensor Set Up

// Demo code for Grove - Temperature Sensor V1.1/1.2
// Loovee @ 2015-8-26

#include <math.h>

const int B = 4275;               // B value of the thermistor
const int R0 = 100000;            // R0 = 100k
const int pinTempSensor = A0;     // Grove - Temperature Sensor connect to A0

#if defined(ARDUINO_ARCH_AVR)
#define debug  Serial
#elif defined(ARDUINO_ARCH_SAMD) ||  defined(ARDUINO_ARCH_SAM)
#define debug  SerialUSB
#else
#define debug  Serial
#endif

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    int a = analogRead(pinTempSensor);

    float R = 1023.0/a-1.0;
    R = R0*R;

    float temperature = 1.0/(log(R/R0)/B+1/298.15)-273.15; // convert to temperature via datasheet

    Serial.print("temperature = ");
    Serial.println(temperature);

    delay(100);
}

The code outputs the current temerature to the serial moniter. alt text

Temperature being meaured:

Analogue measurements using Saleae: