Topic outline

  • Topic 1

    Introduction

    Not that kids read the introduction, but here is a rationale.

    At the end of the year, there is time to reflect on things and there is little pressure on students in the classroom. As for teachers ...

    So, the time is right to investigate outside the curriculum on things that are interesting. 

    Bricoleurs find treasures in charity shops, throw outs and just lying around. Just such a treasure is a model construction crane, purchased at a swap meet for a couple of dollars. 

    The challenge is to remove the cheap and nasty controller and using what has been learnt in class, get it to run

    • from a microcontroller (Arduino)
    • from a micro controller and ld279
    • from a microcontroller and a ld4from a micro controller and motor shield

    or

    • from bluetooth. of a phone.

    The following notes are provided for some guidance.

    construction crane
    • Topic 2

      You need a plan

      Kids are great at rushing in and getting nowhere.systems analysis is a discipline that assists. there are several steps.

      • Trigger
      • analysis
      • design
      • development
      • implementation
      • evaluation
      • document

      first, define the problem and propose a solution.

      second, think about the problem and what you will need. the most important thing in this step is is it feasible?

      third, design what you will do. often this is a pencil and paper flow charts etc. this is where who, what, where, why, when and how much comes into play. also, is the project feasable.                                                                                   

      development is where you produce a working model, write the code etc.

      implementation is the step where put the system to the test. rely do they work first time so there are strategies the account for this.

      does it work? what changes need be made

      the project needs then be documented. along the way an astute pupil will have been taking photos, making video, making notes. 

      it is common to produce a multi media report or one Spiro bound.

      • Topic 3

        Finding the pairs using  a battery

        We expect to know what a circuit is.

        The construction crane has three motors

        turns the crane
        moves gantry crane
        operates the lifting of the bucket

        Cut the leads from the crane to the cheap cotroller

        Challenge : use a 9v battery and find the pairs of wires that control all three motors.

        Document your results

        Image result for battery and toy analog motor
        • Topic 4

          Using a transistor as a switch

          This is quite an important demonstration. It shows how a simple npn transistor can be used as a switch  to control a led.

          Later on we will learn a line of these transistors can be packaged together in what is called an integrated circuit (IC).

          Materials

          • breadboard
          • Arduino board
          • 2n222 transistor
          • 1 K Ohm resistor
          • 100 ohm resistor
          • 3 leads
           

          Set up Arduino and breadboard. 

          Assessment 

          Connect your LED to pin 13 and GND.

          It should flash when you run blink, indicating all is OK.

          • Topic 5

            • Topic 6

              Controlling motors : l293d, the H bridge

              A motor should be able to spin two ways. To accomplish this, there are a number of ways.

              • transistors
              • h briges
              • relays

               


              l293d

              This is a special chip that handles voltages up to 40 volts. It has 8 pins each side and each side can be made to control one device.

              Think of it as two chips in one.

              Vcc is the power input pin. The Arduino only supplies 5 volts so if you want your your motors to run at higher voltage (the point of the chip), connect your voltage to both pins.

              Inputs : there are four of these and these connect to the pins on the Arduino. These are the pins used to control the chip.

              Output : there are also four of these. These connect directly to the motors to run them

              The enable pins must be connected to 5 volts to change their state to HIGH. This enables the chip to function.

              Enable

              1. Interupt1
              2. interupt2
              3. Enableb
              4. Interupt3
              5. Interupt4

              By changing the OUTPUT command to HIGH or LOW, you can open or close the gates in the chip

              h bridge

              h bridge theory and practice

              • Topic 7

                Mr Weber's heads up : connect the motor driver

                OK

                This is the part where the taklking gets much less and the doing gets more.

                First thing is to get your robot to move in a predictable way.

                Program it to move 

                • forward
                • back
                • left (spin)
                • left (sweep)
                • right (spin)
                • right (sweep)
                • reverse
                • halt

                Later we will add a sonic sensor to see if it can avoid obstacles. 

                lln298nThe L297N H bridge

                Smarty pants kids will by now realise they can find examples on the web as well as code.

                Sites like

                • instructables
                • thingaverse
                • youtube
                will give you ideas.

                There are also
                • personal learning networks (PLN's)
                • forums
                • other kids
                Time to get cracking.



                The next part of the jouney is to go completely rogue and try to figure out how you can control a robot by

                • infra red
                • touch sensors
                • wireless
                • gps
                • anything

                Regard you robot not as a one off device but a starting point o investigate how all this fits together.

                • Topic 8

                  • Topic 9

                    Controlling a motor's speed : PWM

                    Motors never run at exactly the same speed, out of the box. There are many techniques to achieve this, but the easiest is Pulse Width Modulation (PWM) To do so, we need to understand PWM.


                     

                    A techy introduction

                    Knowlege : the difference between analog and digital.

                    digitalWrte(ledPin, HIGH);

                    analogWrite(ledPin,i);

                    where i changes from a value of 0 to 255

                    We will start with the blink program just to see if everything is going ok. Pins 3,5,6, 9, 10, 11 can be used for PWM. 

                    Connect you LED to pin 11 and modify your blink code.

                    Run the loop.

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

                    Now we will controll the LED with a transistor. We will use an npn transistor (2n2222 or 547). Use the circuit diagram in the preceding content module to assist you

                     

                    /* Pulse width modulation

                    using one of six pins (11) with tilde (~) next to their pin number to demonstrate
                    pulse width modulation.

                    Note the use of analogWrite on a digital pin
                    Auther mrweber
                    07102018
                    */
                    void setup() {
                    pinMode(11,OUTPUT);
                    }

                    void loop() {
                    for (int i=0; i < 255; i++){
                    analogWrite(11,i);
                    delay(10);
                    }
                    for (int i=255; i > 0; i--){
                    analogWrite(11,i);
                    delay(10);
                    }
                    }

                    Sparkfun circ-03


                    Sparkfuncode03

                    Extra for experts : servo motors

                    circ-11


                    Sparkfuncode11 

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

                      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.

                      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 proceedure
                      {
                      //digitalWrite(ena,HIGH);
                      digitalWrite(int1,HIGH);
                      digitalWrite(int2,LOW);

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

                      }

                      void halt() // halt sub proceedure
                      {
                      //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 conrolling your device during testing.

                      • Topic 11

                        • Topic 12

                          • Topic 13

                            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 usefull to define forward motion in terms of port (left)
                            * and starboard (right).
                            *
                            * This invlved 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);
                            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 proceedure
                            {
                            //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 proceedure
                            {
                            //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.

                             

                            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 14

                              Using the HC-SR04 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 15

                                Running a robot with sonic sensor

                                The hc-sr04 needs 5 volts to operate. Take the Vcc from the L297d motor driver NOT the Arduino board.

                                /*
                                Mr Weber's fifth robot program.

                                the robot goes forwards for 2 seconds then stops,
                                turns left 90 degrees,stops, then right 90 degrees, stops,
                                then back and stops.

                                hint : The hc-sr04 needs 5 volts to operate. Take the Vcc from the L297d motor driver NOT the Arduino board.

                                author mark weber
                                date 6/4/14
                                */

                                int ena = 3; // these relate to the h bridge (L293 chip)
                                int int1 = 4; // to drive the motors
                                int int2 = 5;

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

                                int trigPin = 12; // check the hc-sr04 sonic sensor
                                int echoPin = 11; // for the pinouts

                                long duration, distance;

                                long x = 0;

                                void setup() {

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

                                pinMode (ena,OUTPUT);
                                pinMode (int1,OUTPUT);
                                pinMode (int2,OUTPUT);


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

                                }

                                void loop()
                                {
                                distance = sonic(x);
                                if (distance >= 200 || distance <= 0){ // testing
                                Serial.println("Out of range main");
                                }
                                else {
                                Serial.print(distance); // function working
                                Serial.println(" cm main");
                                }

                                if (distance > 10)
                                {

                                forward();
                                }
                                else
                                {
                                halt();
                                back();
                                halt();
                                left();
                                }
                                delay(500);
                                }

                                long sonic(long x) // calls the function sonic here
                                {

                                digitalWrite(trigPin, LOW); // Added this line
                                delayMicroseconds(2); // Added this line
                                digitalWrite(trigPin, HIGH);
                                delayMicroseconds(10); // Added this line
                                digitalWrite(trigPin, LOW);
                                duration = pulseIn(echoPin, HIGH);
                                distance = (duration/2) / 29.1;

                                if (distance >= 200 || distance <= 0){
                                Serial.println("Out of range");
                                }
                                else {
                                Serial.print(distance);
                                Serial.println(" cm");
                                }
                                delay(500);
                                return distance;
                                }


                                void halt()
                                {
                                digitalWrite(ena,HIGH);
                                digitalWrite(int1,LOW);
                                digitalWrite(int2,LOW);
                                digitalWrite(enb,HIGH);
                                digitalWrite(int3,LOW);
                                digitalWrite(int4,LOW);
                                delay(500);
                                }


                                void back()
                                {
                                digitalWrite(ena, HIGH);
                                digitalWrite(int1,LOW);
                                digitalWrite(int2,HIGH);
                                digitalWrite(enb,HIGH);
                                digitalWrite(int1,LOW);
                                digitalWrite(int4,HIGH);
                                delay(500);
                                }

                                void left()
                                // goes right
                                {
                                digitalWrite(ena, HIGH);
                                digitalWrite(int1,LOW);
                                digitalWrite(int2,HIGH);
                                digitalWrite(enb, HIGH);
                                digitalWrite(int3,HIGH);
                                digitalWrite(int4,LOW);
                                delay(500);
                                }

                                void right()
                                // goes backwards
                                {

                                digitalWrite(ena, HIGH);
                                digitalWrite(int1,HIGH);
                                digitalWrite(int2,LOW);
                                digitalWrite(enb, HIGH);
                                digitalWrite(int3,LOW);
                                digitalWrite(int4,HIGH);

                                }

                                void forward()
                                {
                                digitalWrite(ena, HIGH);
                                digitalWrite(int1,HIGH);
                                digitalWrite(int2,LOW);
                                digitalWrite(enb, HIGH);
                                digitalWrite(int3,HIGH);
                                digitalWrite(int4,LOW);
                                delay(500); // this is shorter
                                }

                                • Topic 16

                                  IR detector

                                  ir sensor

                                  TCRT5000


                                  tcrt5000

                                  //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=analogRead (A0); // note use of Arduino analog port
                                  Serial.println(sensor);
                                  delay(1000);

                                  if (sensor < 500)
                                  {
                                  Serial.println("hello less than 500, it must be light");
                                  }
                                  else
                                  {
                                  Serial.println("we are in the dark, greater than 500");
                                  }
                                  }

                                  Data sheet
                                  • Topic 17

                                    • Topic 18