Topic outline

  • General

    Arduino : digital sensors

    Each type is controlled by three systems

    • Sensors
    • Actuators
    • Control

    The sensors interact with their environment and communicate with the control system in a feedback loop. They control system activates the actuator (eg motor) to change the state of the system.
  • Topic 1


    This follows from a simple Introduction to Electronics.

    The world of microelectronics is a world of volts and ground. We defined voltage a a force that moves current around a circuit and resistance as the property of a body to oppose this flow.

    Voltage also can be thought of as a signal. When a high enough voltage is applied, current will flow. This can then be thought of as a signal.

    Data can be moved along a line in

    • serial
    • parallel

    The signal can also be moved as 

    • digital
    • analog

    Arduino has a circuit in the board that can convert a digital signal to analog and the reverse.

    • Topic 2

      Simple Electrical Theory

      In electrical theory, current flows from positive to negative. Positive is designated (+) and is usually red. Negative or ground is designated (-) and can be black or brown.

      If this is a 5 volt battery, it is a analogy for an input/output for an Arduino digital signal.

      digitalWrite (pin, HIGH) sets the port to +5v
      digitalWrite (pin, LOW) sets the port to 0v  (theoretically)

      digitalRead(pin) will return a value of HIGH or LOW 

      circuit diagram

      • Topic 3

        Digital sensors

        A sensor is a device that detects and responds to some type of input from the physical environment. The specific input could be light, heat, motion, moisture, pressure, or any one of a great number of other environmental phenomena.

        A digital sensor detects only two values, ON/OFF, HIGH/LOW, 0, 5V

        We can use several types
        • Touch
        • Infra red
        • Sonic
        • Touch

        The point of a sensor is to trigger an actuator?.  An actuator is any type of motor that is responsible for moving or controlling a mechanism or system.

        We use several types
        • Serial motor
        • Stepper motor
        • Servo motor
        • Solenoid

        • Topic 4

          Voltage dividers

          A voltage divide is a simple method of controlling current in a circuit.

          If there is a constant input voltage, then provided the total resistance remains the same, the same current flows through both resistors.

          It follows that for Ohms law to be obeyed, the voltage must be split in the ratio of the resistors.

          eg input voltage = 6 volts

              R1 = 1 Ohm

              R2 = 2 Ohms

          Volts in R1 = 2 volts

          Volts in R2 = 4 volts

          Changing the resistors in a circuit is a common way to control current for a LED. 

          voltage divider
          This demonstrates how a sensor can be created. By adjusting the input voltage to the Arduino to 5V, we can measure changes in a sensor.
          "If it bleeds, we can kill it.
          If it has a voltage, we can measure it.
          If we can measure it, we can infer a value"
          • Topic 5

            A strategy for prototyping

            Six key steps

            1. Find out about your senso(data sheets)

            2. Determine equations that convert your ADC readings to what you are measuring.

            3. Write a simple sketch to test all is working

            4. Verify if data is correct, maybe callibrate instrument.

            5. Integrate code into project

            6. Document

            Reference :

            Arduino projects to save the world

            Emery Primeuax 

            ISBN-13 978-1-4302-3623-8

            ISBN-13  978-1-4302-3623-5

            The hardest thing to do is convince newbies of the importance of documentation. A pen and notebook are very handy to keep a whole bunch of facts at hand.

            When code is completed, make notes in the sketch to remind yourself of what you did.

            Believe me, in six months you might not remember what you have noted, even if you wrote it!

            • Topic 6

              Using a touch sensor

              // A simple ON/OFF collision switch

              const int ledPin = 13;
              const int inPin = 8;
              int reader = 0;

              void setup() {
              // put your setup code here, to run once:
              pinMode(ledPin, OUTPUT);
              pinMode(inPin, INPUT);

              void loop() {
              // put your main code here, to run repeatedly:
              reader = digitalRead(inPin);

              if (reader == HIGH) {
              digitalWrite(ledPin, HIGH);
              else {
              digitalWrite(ledPin, LOW);
              collision switch Using Arduino code

              && Logical AND
              // if the switch is down, the LED is on
              • Topic 7

                IR detector



                //this is a program to test if we can read a sensor
                //we used a tcrt5000 sensor i bought off alibaba
                //in a set with an ir led
                //the key to the exercise was the balance between the two resistors in the circuit diagram

                int sensor; // use an integer and read it continually

                void setup()
                Serial.begin(9600); //read serial port

                void loop()
                sensor=digitalRead (8); // note use of Arduino digital port

                if (sensor == HIGH)
                Serial.println("hello, it must be light");
                Serial.println("we are in the dark");

                Data sheet
                • Topic 8

                  The problem of the bouncing switch

                  • Topic 9

                    The debounce problem

                    In the world of switches and input, at a tiny level, switches often take a couple of goes at connecting. This is known as debounce. So a robot sensing an object may get different reading due to 
                    • debounce
                    • reflection
                    • noise in the circuit
                    He is a some code that explains how to cater for this.

                    Full explanation here
                    const int buttonPin = 2;    // the number of the pushbutton pin
                    const int ledPin = 13;      // the number of the LED pin

                    int ledState = HIGH;         // the current state of the output pin
                    int buttonState;             // the current reading from the input pin
                    int lastButtonState = LOW;   // the previous reading from the input pin

                    // the following variables are unsigned long's because the time, measured in miliseconds,
                    // will quickly become a bigger number than can be stored in an int.
                    unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
                    unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

                    void setup() {
                      pinMode(buttonPin, INPUT);
                      pinMode(ledPin, OUTPUT);

                      digitalWrite(ledPin, ledState);   // set initial LED state

                    void loop() {
                      int reading = digitalRead(buttonPin);    // read the state of the switch into a local variable:
                      // check to see if you just pressed the button
                      // (i.e. the input went from LOW to HIGH),  and you've waited
                      // long enough since the last press to ignore any noise:

                      // If the switch changed, due to noise or pressing:
                     // millis returns the number of milliseconds since the Arduino board began running the current program.
                      if (reading != lastButtonState) { 
                        lastDebounceTime = millis();  // reset the debouncing timer

                      if ((millis() - lastDebounceTime) > debounceDelay) {
                        // whatever the reading is at, it's been there for longer
                        // than the debounce delay, so take it as the actual current state:

                        // if the button state has changed:
                        if (reading != buttonState) {
                          buttonState = reading;

                              if (buttonState == HIGH) { // only toggle the LED if the new button state is HIGH
                            ledState = !ledState;
                      digitalWrite(ledPin, ledState);   // set the LED:
                      lastButtonState = reading;     // save the reading.  Next time through the loop, it'll be the lastButtonState:
                    • Topic 10

                      Debounce the circuit

                      const int buttonPin = 8; // the number of the pushbutton pin
                      const int ledPin = 13; // the number of the LED pin

                      int ledState = HIGH; // the current state of the output pin
                      int buttonState; // the current reading from the input pin
                      int lastButtonState = LOW; // the previous reading from the input pin
                      unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
                      unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers

                      void setup() {
                      pinMode(buttonPin, INPUT);
                      pinMode(ledPin, OUTPUT);

                      digitalWrite(ledPin, ledState); // set initial LED state

                      void loop() {
                      int reading = digitalRead(buttonPin); // read the state of the switch into a local variable:
                      if (reading != lastButtonState) {
                      lastDebounceTime = millis(); // reset the debouncing timer
                      if ((millis() - lastDebounceTime) > debounceDelay) {
                      if (reading != buttonState) {
                      buttonState = reading;
                      if (buttonState == HIGH) { // only toggle the LED if the new button state is HIGH
                      ledState = !ledState;
                      digitalWrite(ledPin, ledState); // set the LED:
                      lastButtonState = reading; // save the reading. Next time through the loop, it'll be the lastButtonState:
                      • Topic 11

                        • Topic 12

                          Analogue to digital converters

                          Arduino's have an analogue to digital converter built in to the chip. The analogue inputs have the letter "A" in front of the input.
                          analogue to digital Sparkfun's explaination
                          • Topic 13

                            Sonic sensor



                            HC-SR04 Ping distance sensor]
                            VCC to arduino 5v GND to arduino GND
                            Echo to Arduino pin 13 Trig to Arduino pin 12

                            #define trigPin 13
                            #define echoPin 12

                            int duration;
                            int distance;

                            void setup() {
                            Serial.begin (9600);
                            pinMode(trigPin, OUTPUT);
                            pinMode(echoPin, INPUT);

                            void loop() {
                            long duration, distance;
                            digitalWrite(trigPin, LOW);
                            digitalWrite(trigPin, HIGH);
                            digitalWrite(trigPin, LOW);
                            duration = pulseIn(echoPin, HIGH);
                            distance = (duration/2) / 29.1;

                            Serial.println(" cm");


                            • Topic 14

                              • Topic 15

                                Sub system : the Arduino board

                                arduino board

                                The Arduino road map.
                                can you identify the following on your board
                                • the ATMega328 chip
                                • the 16Mhz clock
                                • reset switch
                                • the port you connect to your computer
                                • digital i/o ports
                                • analogue i/o ports
                                • ports where power is available
                                • external power port
                                Smarty pants speedy kids might wish to get straight to the "good stuff". If you have an Arduino kit, here is a set of exercises for you to try.

                                Sparkfun inventors' guide(PDF)

                                Reading Assignment

                                About the board from the Arduino web site

                                • Topic 16

                                  • Topic 17

                                    Using a digital sensor on a robot

                                    /* a small program written for leanne so she has something for her display at ashdale
                                    it has a small robot operating an ir sensor on a white tube board inside a circle of black tape.
                                    as it senses the black tape, it stops, backs up, turns and goes forward
                                    author mrweber
                                    date 08092016
                                    int int1 = 5;
                                    int int2 = 6;
                                    int int3 = 9;
                                    int int4 = 10;
                                    int speedy = 80; // set analog speed here
                                    int timex = 3000; // how long to wait for the robot to start
                                    int inPin = 8; // the number of the input pin
                                    int outPin = 13; // the number of the output pin
                                    int reader; // the current reading from the input pin
                                    void setup() {
                                    pinMode(outPin, OUTPUT);
                                    pinMode(inPin, INPUT);
                                    // Serial.begin(9600); // this is here for debugging
                                    void loop() {
                                    // Serial.println(reader); // this is here for debugging
                                    reader = digitalRead(inPin); // read the surface under sensor

                                    if (reader == HIGH) { // ie i am reading white under sensor
                                    digitalWrite(outPin, reader);
                                    else { // ie i am reading black under sensor
                                    digitalWrite(outPin, reader);
                                    void reverse() { // reverse sub procedure
                                    digitalWrite(int1, LOW);
                                    analogWrite(int2, speedy);
                                    digitalWrite(int3, LOW);
                                    analogWrite(int4, speedy);
                                    void forward() // forward sub proceedure
                                    analogWrite(int1, speedy);
                                    digitalWrite(int2, LOW);
                                    analogWrite(int3, speedy);
                                    digitalWrite(int4, LOW);
                                    void halt() // halt sub proceedure
                                    digitalWrite(int1, LOW);
                                    digitalWrite(int2, LOW);
                                    digitalWrite(int3, LOW);
                                    digitalWrite(int4, LOW);
                                    void left() {
                                    digitalWrite(int1, speedy);
                                    digitalWrite(int2, LOW);
                                    digitalWrite(int3, LOW);
                                    digitalWrite(int4, speedy);
                                    delay(random(100, 500)); // random spins
                                    • Topic 18

                                      • Topic 19


                                        • Topic 20

                                          • Topic 21

                                            • Topic 22