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

    Analog v Digital

    Analog is a continuous signal

    Digital is either ON of OFF

    Sparkfun explains
    An Arduino uses the command digitalRead to monitor digital input for instance a door is open or closed, and analogRead for continuous data such as a thermometer..
    • Topic 2

      A simple circuit

      The simplest circuit is a power supply, a switch, a globe and some leads. Note there is some reisistance in the circuit which is usually shown as well
      • Topic 3

        Button sensor

        The button circuit demonstrates the use of 

        • serial monitor
        • pull up/pull down resistors

        Arduino button explanation

        Using the serial monitor

        / Button
        *
        Turns on and off a light emitting diode(LED) connected to digital
        pin 13, when pressing a pushbutton attached to pin 2.

        The circuit:
        * LED attached from pin 13 to ground
        * pushbutton attached to pin 2 from +5V
        * 10K resistor attached to pin 2 from ground

        http://www.arduino.cc/en/Tutorial/Button
        */
        // constants dont change
        const int buttonPin = 2; // the number of the pushbutton pin
        const int ledPin = 13; // the number of the LED pin

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

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

        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:
        digitalWrite(ledPin, HIGH);
        Serial.println("hi"); // use serial to check the state of the button
        } else {
        // turn LED off:
        digitalWrite(ledPin, LOW);
        Serial.println("low"); // use serial to check the state of the button
        }
        Serial.write(buttonState);
        Serial.println("out of the loop"); // use serial to check the state of the button
        delay(500);
        }

        Investigate debounce
        • Topic 4

          Collision sensor

          • 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

              Getting the robot to go straight : PWM

              For various reasons, the voltage supplied to each motor is different. There is a way to vary the voltage using pulse width modulation. For a full explanation, check out youtube.

              The key to understanding PWM is to understand the difference between a digital and analog signal. The commands for each are

              digitalWrte(ledPin, HIGH);

              analogWrite(ledPin,i);

              where i changes from a value of 0 to 255

              A digital signal in Arduino is either a 5v or 0v prot. An analog signal in Arduino is 5v spread between 0 - 255 steps. Analog can be reprogrammed in pins 3,5,6, 9, 10, 11 or accessed directly with pins A0 - A5.

              Here is some code that demonstrates the use of PWM using an Arduino, 2 motors and an L298n motor driver.

               

              A techy introduction

              Knowlege : the difference between analog and digital.

              /* This is a first attempt to get my tank robot to go straight.
              * It seems to meed more power on the starboard side than the port.
              * It is useful to define forward motion in terms of port (left)
              * and starboard (right).
              *
              * This involved pulse width modulation. not the difference between
              * analogWrite() and digitalWrite() commands
              *
              * Note if the PWM is not high enough, there might not be enough power
              * to turn over your motors
              *
              * mrweber
              * 14/5/16
              */


              // int ena = 4;
              int int1 = 5;
              int int2 = 6;

              // int enb = 8;
              int int3 =9;
              int int4 = 10;

              int timex = 3000; //runs for 3 seconds
              boolean run;

              void setup() {
              //pinMode (ena,OUTPUT);  // for old style l298 boards
              pinMode (int1,OUTPUT);
              pinMode (int2,OUTPUT);

              //pinMode (enb,OUTPUT);
              pinMode (int3,OUTPUT);
              pinMode (int4,OUTPUT);

              run = true;
              }

              void loop()
              {
              delay(3000);
              if (run)
              {
              forward();
              delay (4000);
              halt();
              run = false; // runs only one time
              }
              }

              void forward() // forward sub procedure
              {
              //starboard motor
              analogWrite(int1,180); // <==== 100% 
              digitalWrite(int2,LOW);

              //port motor
              analogWrite(int3,150); // < === 83% this balances the load
              analogWrite(int4,LOW);

              }
              void halt() // halt sub procedure
              {
              //digitalWrite(ena,HIGH);
              digitalWrite(int1,LOW);
              digitalWrite(int2,LOW);

              //digitalWrite(enb,HIGH);
              digitalWrite(int3,LOW);
              digitalWrite(int4,LOW);

              }

              Sparkfun circ-01


              Sparkfuncode01 

              Skill : be able to write subroutines in C and call them from the main program.

               This is only a prototype sketch. The next step is to use parameter passing.

              Sparkfun circ-03


              Sparkfuncode03

              Extra for experts : servo motors

              circ-04 


              Sparkfuncode04

              The dont argue : relays

              circ-11


              Sparkfuncode11 

              Skill : Be able to search out a datasheet on the web and deduce from information.
              • Topic 7

                IR detector

                TCRT5000

                tcrt5000

                4.7 K Ω resistor is the same as 4700 ohm resistor

                //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
                Serial.println(sensor);    // we can also use it as an analog sensor
                delay(20);

                if (sensor == HIGH)
                {
                Serial.println("hello, ==> it must be dark"); // comment this out as it
                }
                else
                {
                Serial.println("we are in the light"); // takes time to print onscreen
                }
                }

                Data sheet
                • Topic 8

                  nnn

                  /*
                  * L293
                  *
                  * This is my development sketch for a small toy tank i found
                  * at good sammies for $2
                  *
                  * It works by passing parameters as global variables
                  *
                  * Mark Weber

                  *
                  */
                  // Declare data types and variables
                  int motorPin1 = 5; // choose the pin for the motor
                  int motorPin2 = 6; // choose the pin for the motor
                  int motorPin3 = 10; // choose the pin for the motor
                  int motorPin4 = 11; // choose the pin for the motor


                  int lowValue = 0;               // global variable
                  int highValue = 125;           // global variable

                  boolean yes = true;

                  int delayTime = 200;           // global variable

                  void setup() {
                  pinMode(motorPin1, OUTPUT); // declare as output
                  pinMode(motorPin2, OUTPUT); // declare as output
                  pinMode(motorPin3, OUTPUT); // declare as output
                  pinMode(motorPin4, OUTPUT); // declare as output

                  }
                  void loop(){
                  while (yes == true)
                  {
                  halt();
                  delay(delayTime);
                  one_way();
                  delay(delayTime);
                  halt();


                  delay (delayTime);
                  other_way();
                  delay(delayTime);
                  halt();
                  }
                  }
                  void one_way(){
                  // first one way

                  analogWrite(motorPin1, lowValue);
                  analogWrite(motorPin2, highValue);
                  analogWrite(motorPin3, lowValue);
                  analogWrite(motorPin4, highValue);


                  }

                  void other_way(){
                  // then the other
                  analogWrite(motorPin1, highValue);
                  analogWrite(motorPin2, lowValue);
                  analogWrite(motorPin3, highValue);
                  analogWrite(motorPin4, lowValue);
                  }

                  void halt (){
                  // stop the motor
                  analogWrite(motorPin1, lowValue);
                  analogWrite(motorPin2, lowValue);
                  analogWrite(motorPin3, lowValue);
                  analogWrite(motorPin4, lowValue);
                  yes = false;
                  }

                  /*

                  code snippet passing parameters by local variable

                  void loop() {

                  turn (125, 0, 0, 0, 1000); // values passed to procedure

                  }


                  void turn(int x1, int x2, int x3, int x4, int delayx)  // local variable declaration
                  {
                  analogWrite(motorPin1, x1);
                  analogWrite(motorPin2, x2);
                  analogWrite(motorPin3, x3);
                  analogWrite(motorPin4, x4);
                  delay (delayx);
                  }

                  • Topic 9

                    The problem of the bouncing switch

                    • Topic 10

                      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
                      */
                       http://www.arduino.cc/en/Tutorial/Debounce
                       */
                      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:
                      }
                      button
                      • Topic 11

                        Challenge 1 : Getting it to run and stop

                        Copy and paste the following code into your Arduino IDE.

                        The objective is to make sure the wheel motors are plugged into the motor driver so they propel the vehicle forward. Later we can use parameter passing to make the code easier

                        Check int 1 is connected to pin 9 etc. If the robot does not go forward, reverse the leads on motor a and/or b.

                        Note : it is not unusual for your robot not to travel in a straight line. You need to balance the load on both motors. There are several techniques, and we will cover one, pulse width modulation, later.


                        /*
                        Robot 1 : Documentation

                        This is the first program for running the simple robot.
                        We just want to make sure the motors spin the right way and the robot moves forward.

                        Note the use of the ena, enb. In the old version of l298, there were 6 inputs. ena and enb
                        are set to HIGH. these can be set by hardware and hence are commented out.

                        Depending on your driver, you can compile these commands or not. Use find
                        and replace to remove the comment out directive if you wish.
                        */

                        // int ena = 4;
                        int int1 = 5;
                        int int2 = 6;

                        // int enb = 8;
                        int int3 =9;
                        int int4 = 10;

                        int timex = 3000; //runs for 3 seconds
                        boolean run;

                        void setup() {
                        //pinMode (ena,OUTPUT);
                        pinMode (int1,OUTPUT);
                        pinMode (int2,OUTPUT);

                        //pinMode (enb,OUTPUT);
                        pinMode (int3,OUTPUT);
                        pinMode (int4,OUTPUT);

                        run = true;
                        }

                        void loop() {
                        if (run)
                        {
                        forward(); // sets the robot forward.
                        // check motors are spinning in same direction
                        delay(timex);
                        halt(); // stops the code running
                        run = false;
                        }
                        }

                        void forward() // forward sub procedure
                        {
                        //digitalWrite(ena,HIGH);
                        digitalWrite(int1,HIGH);
                        digitalWrite(int2,LOW);

                        //digitalWrite(enb, HIGH);
                        digitalWrite(int3,HIGH);
                        digitalWrite(int4,LOW);

                        }

                        void halt() // halt sub procedure
                        {
                        //digitalWrite(ena,HIGH);
                        digitalWrite(int1,LOW);  
                        digitalWrite(int2,LOW);

                        //digitalWrite(enb,HIGH);
                        digitalWrite(int3,LOW);
                        digitalWrite(int4,LOW);

                        }

                        Solder a switch on the positive (red) wire, between the battery pack and the boards. drill and appropriately sized hole and screw the switch in. 

                        This is a useful innovation for controlling your device during testing.

                        • 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

                            sketch

                            /*
                            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);
                            delayMicroseconds(20);
                            digitalWrite(trigPin, HIGH);
                            delayMicroseconds(20);
                            digitalWrite(trigPin, LOW);
                            duration = pulseIn(echoPin, HIGH);
                            distance = (duration/2) / 29.1;

                            Serial.print(distance);
                            Serial.println(" cm");

                            delay(500);
                            }

                            • 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

                                  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);
                                  delay(timex);
                                  // 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);
                                  forward();
                                  }
                                  else { // ie i am reading black under sensor
                                  digitalWrite(outPin, reader);
                                  halt();
                                  reverse();
                                  left();
                                  }
                                  }
                                  void reverse() { // reverse sub procedure
                                  digitalWrite(int1, LOW);
                                  analogWrite(int2, speedy);
                                  digitalWrite(int3, LOW);
                                  analogWrite(int4, speedy);
                                  delay(500);
                                  }
                                  void forward() // forward sub proceedure
                                  {
                                  analogWrite(int1, speedy);
                                  digitalWrite(int2, LOW);
                                  analogWrite(int3, speedy);
                                  digitalWrite(int4, LOW);
                                  delay(10);
                                  }
                                  void halt() // halt sub proceedure
                                  {
                                  digitalWrite(int1, LOW);
                                  digitalWrite(int2, LOW);
                                  digitalWrite(int3, LOW);
                                  digitalWrite(int4, LOW);
                                  delay(200);
                                  }
                                  void left() {
                                  digitalWrite(int1, speedy);
                                  digitalWrite(int2, LOW);
                                  digitalWrite(int3, LOW);
                                  digitalWrite(int4, speedy);
                                  delay(random(100, 500)); // random spins
                                  }
                                  • Topic 17



                                    • Topic 18

                                      x

                                      • Topic 19