Topic outline

  • General

    Arduino detectors

  • Topic 1

    Introduction : Sensors and Actuators

    A sensor is a device that according it's environment, can change a physical characteristic. This can then be converted to a scale and compared to a standard.

    A simple thermometer has mercury in a thin tube that expands and contracts according to the heat content of the liquid. a simple standard is to compare it with the freezing point of water (0o C) and boiling point  (100C). Humans a probable best suited to live around 25C.

    As the microcontroller is a very simple device that can process electrical signals. For an Arduino to sense light, temperature, or other physical quantities, it needs something that can convert them into a voltage.

    That is a sensor.

    Sensors then trigger an actuator.

    Once the sensors have been read, the device has the data needed to decide how to react. The decision-making process is handled by the microcontroller, and the reaction is performed by actuators. These functions could be performed by a light or an electric motor

    A feedback loop uses sensors and actuators to control a process. It is the part section of a control system which allows for feedback and self-correction while adjusting its operation according to the difference between the actual output and the desired output.

    There are many examples of such loops. A washing machine stops filling when it has enough water, aircraft on autopilot changes it's flight or someone breathes harder when they exercise.

    A sensor changes its properties (eg resistance) according to it's environment. An actuator (eg a motor) changes in response to the detector. 
    • Topic 2

      the analog signal

      An anolog signal is continuous over time and can take on any value in a range.
      • Topic 3

        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 4


          A very simple demonstration of how an analog sensor works. It is presented as written.

          // Ultra simple thermometer
          // by Leon Harris
          // Version 0.1

          /* This thermometer is based around the fact that silicon diodes
          have a temperature coefficient of about -2mv /0C. So if supplied
          with a constant current source, the voltage across a diode will
          decrease by -2mV for every degree centigrade. All you need to
          do is to supply it with a constant current source. In ARduino terms,
          this means a resistor from a regulated power supply to inject 0.5 to 1mA.
          I am to cheap to buy a resistor, so I have instead programmed one
          of the internal 10K pull up resistors on Analog input 0, and simple wired
          the anode of the diode to A0, and the cathode to A1, which I set to
          digital mode and to output 0v (ie connect the diode to ground).
          Thus and Arduino plus 1 part can make a thermometer!


          int Temperature=0;
          void setup() {
            // put your setup code here, to run once:

          void loop() {
            // put your main code here, to run repeatedly:

        • Topic 5

          Simple temperature measurement

          This exercise reads the middle pin to the analogue 0 pin directly. There is no need to manipulate the voltage using a voltage dividers, reading directly does the trick. Use the serial window in your Arduino sketch application (Tools -> Serial Port) to see the results.

          Note the use of read and readln to format the results. Using the concatenation + operator and the string "," later on means you can create output that can be read direct to a .csv file that can be read directly into a spreadsheet.

          Also not this sensor reads in Centigrade, and the neat conversion formula to change it to Fahrenheit (for Americans).

          Check the website for a discussion on analogReference (ahref).

          LM 35 reference

          /* code to read a centigrade sensor ln35dz and report 
          to the serial window */

          int analogPin = 0;
          int readValue = 0;
          float temperatureC = 0;
          float temperatureF = 0;

          void setup() {

          void loop() {
          readValue = analogRead(analogPin);
          temperatureC = (readValue * 0.0049);
          temperatureC = temperatureC * 100;
          temperatureF = (temperatureC * 1.8) + 32;
          Serial.print("Temperature: ");
          Serial.print("C ");

          lm 35
          /* using ahref to get a more accurate reading

          float tempC;
          int reading;
          int tempPin = 0;

          void setup()

          void loop()
          reading = analogRead(tempPin);
          tempC = reading / 9.31;
          • Topic 6

            Simple data logging 

            Write to the serial window, cut and paste into a spreadsheet.
            this code reads a light dependant resistor
            and then prints it out in the serial window
            of the Arduino SDK

            int reading1; //connected to analogue pin 01

            void setup()
            Serial.begin(9600); // this communicates
            //with the serial window
            pinMode(A0, INPUT);
            void loop()
            reading1 = analogRead(A0);
            Serial.print("sensor =>, ");// note print
            // note comma
            Serial.println(reading1); // and println
            • Topic 7

              dallas 18B20

              This is a simple sketch that read a single sensor, ds18b20

              instructions at


              #include <OneWire.h>
              #include <DallasTemperature.h>

              // Data wire is plugged into port 2 on the Arduino
              #define ONE_WIRE_BUS 2

              // Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
              OneWire oneWire(ONE_WIRE_BUS);

              // Pass our oneWire reference to Dallas Temperature.
              DallasTemperature sensors(&oneWire);

              void setup(void)
              // start serial port
              Serial.println("<< and so it begins >>");
              // Start up the library

              void loop(void)
              // call sensors.requestTemperatures() to issue a global temperature
              // request to all devices on the bus
              Serial.print("Requesting temperatures...");
              sensors.requestTemperatures(); // Send the command to get temperatures
              Serial.print("Temperature for the device 1 (index 0) is: ");


              This sketch will display a reading in degrees centigrade on a Nokia 5110 screen

              pinouts nokia 5111
              1 2 3 4 5 6 7 8
              r c d d c v l g
              s e c i l c g n
              t n k c t d

              code from


              hacked by numbat mark


              #include <OneWire.h>
              #include <DallasTemperature.h>
              #include <LCD5110_Graph.h>

              #define ONE_WIRE_BUS 2 // it reads the digital pin

              LCD5110 lcd(3,4,5,6,7);

              extern unsigned char SmallFont[];
              extern unsigned char BigNumbers[];
              extern uint8_t temperatureIcon[];

              char temperatureF[6];
              char temperatureC[6];

              OneWire oneWire(ONE_WIRE_BUS);

              DallasTemperature sensors(&oneWire);

              float tempC = 0;
              float tempF = 0;

              void setup(void)

              void loop(void)

              // lcd.drawBitmap(0, 0, temperatureIcon, 84, 48);


              tempC = sensors.getTempCByIndex(0);
              tempF = sensors.toFahrenheit(tempC);

              // convertToString(tempF);
              convertToString(tempC); //Uncomment this for degrees Celsius
              // lcd.print(temperatureF,25,11);


              void convertToString(float number)
              //dtostrf(number, 3, 1, temperatureF);
              dtostrf(number, 3, 1, temperatureC);

              • Topic 8

                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
                Red POS to Arduino pin 11
                Green POS to Arduino pin 10
                560 ohm resistor to both LED NEG and GRD power rail
                More info at:
                Original code improvements to the Ping sketch sourced from
                Some code and wiring inspired by

                #define trigPin 13
                #define echoPin 12
                #define led 11
                #define led2 10

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

                void loop() {
                long duration, distance;
                digitalWrite(trigPin, LOW); // Added this line
                delayMicroseconds(2); // Added this line
                digitalWrite(trigPin, HIGH);
                // delayMicroseconds(1000); - Removed this line
                delayMicroseconds(10); // Added this line
                digitalWrite(trigPin, LOW);
                duration = pulseIn(echoPin, HIGH);
                distance = (duration/2) / 29.1;
                if (distance < 10) { // This is where the LED On/Off happens
                digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off
                else {
                if (distance >= 200 || distance <= 0){
                Serial.println("Out of range");
                else {
                Serial.println(" cm");

                • This topic

                  Topic 9

                  I2C communications

                  • Topic 10

                    Measure barometric pressure : BP180

                    • Topic 11

                      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=analogRead (A0); // note use of Arduino analog port

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

                      Data sheet
                      • Topic 12

                        Arduino weather station

                        The DHT11 is a very low-power device – it consumes no more than 2.5-milliamps (2.5mA) of current. The digital I/O (input/output) ports of an Arduino can supply up to 40mA, so if we set the Arduino’s D3 pin high (that is, to 5V) and the D6 pin to GND (0V), we can power a device between those two pins, up to 40mA.

                        The DHT11 can measure temperature from 0 to 50-degreesC and 20-90% relative humidity (the DHT22, also called the AM2302, can go from -40 to 80-degreesC and 0-100% relative humidity). But to read the data from sensor’s Pin 2, we need to apply a five-kiloohm (5kO) resistor connecting that pin to the 5V supply line or ‘rail’ – in this instance, it’s called a ‘pull-up resistor’ because it’s ‘pulling up’ the sensor’s Pin 2 to the supply rail.

                        But this is where we’ll use another hidden trick – the Arduino Uno’s I/O pins all have built-in pull-up resistors and we activate the one on Pin D4 using one line of code:

                        pinMode(4, INPUT_PULLUP);

                        That tells the Arduino we want Pin D4 as an input (so we can read data from the sensor) and to activate its internal pull-up resistor. Pretty sneaky, huh? The upshot of all this is we’ve made it incredibly easy to install the DHT11 into the Arduino – all we need to do is just plug it in! With the DHT11 label facing in, it plugs into pins D3 to D6.

                        Grab the zip file from our webpage Download it, unzip it and copy the contents of the libraries subfolder into the libraries folder of the Arduino IDE. Restart the IDE and load in the ab02_temphumid_v1.ino sketch file. Remove the DHT11 sensor if you have it plugged into the Arduino board, then plug the board into your computer via the USB cable. (If using a Freetronics Eleven board, get the driver software first from

                        In the IDE menu, select ‘Tools’, ‘Board’, choose ‘Arduino Uno’, then select ‘Tools’ again, but this time, go ‘Serial Port’ and select the COM port for your board (it won’t likely be COM1 – that’s your mouse!).


                        A simple weather station


                        /* Arduino Basics - #2 - DHT11 temp/humidity sensor
                        * Code by Darren Yates - 26-Sep-2014

                        #include <dht.h>
                        dht DHT;
                        int DHTread, DHTtemp, DHThumid;
                        int DHTpin = 4;

                        void setup() {
                        pinMode (4, INPUT_PULLUP);
                        pinMode (3, OUTPUT);
                        pinMode (6, OUTPUT);
                        digitalWrite(3, HIGH);
                        digitalWrite (6, LOW);

                        Serial.begin(57600); // note the baud rate
                        Serial.println("DHT11 temperature/humidity sensor");
                        DHTread = DHT.read11(DHTpin); // read pin 4

                        switch (DHTread) {
                        case DHTLIB_OK:
                        Serial.println("=== Sensor OK.");
                        Serial.println("=== ERROR: Problem with sensor.");

                        void loop() {

                        DHTread = DHT.read11(DHTpin); // read pin 4
                        DHTtemp = DHT.temperature;
                        DHThumid = DHT.humidity;
                        Serial.print("Temperature: ");
                        Serial.print(DHTtemp); Serial.print("-degreesC ");
                        Serial.print(" Relative Humidity: "); Serial.print(DHThumid);

                        • Topic 13

                          Using a TV remote

                          • Topic 14


                            Moisture sensor
                            • Topic 15

                              • Topic 16

                                • Topic 17

                                  • Topic 18

                                    • Topic 19