Topic outline

  • General

    Arduino detectors

  • Topic 1

    Introduction : Sensors and Actuators

    Sensors and actuators are electronic components that allow a piece of electronics to interact with the world.

    As the microcontroller is a very simple computer, it can process only electric signals (a bit like the electric pulses that are sent between neurons in our brains). For it to sense light, temperature, or other physical quantities, it needs something that can convert them into electricity.

    In our body, for example, the eye converts light into signals that get sent to the brain using nerves. In electronics, we can use a simple device called a light-dependent resistor (an LDR or photoresistor) that can measure the amount of light that hits it and report it as a signal that can be understood by the microcontroller.

    Once the sensors have been read, the device has the information needed to decide how to react. The decision-making process is handled by the
    microcontroller, and the reaction is performed by actuators. In our bodies, for example, muscles receive electric signals from the brain and convert
    them into a movement. In the electronic world, these functions could be performed by a light or an electric motor

    This can be thought of as a feedback loop. It can be defined as a 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 auto pilot changes it;s flight or someone breathes harder when they exercise.

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

      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 3

        A simple switch circuit

        • 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
          • Topic 5

            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 6

              Pull up resistors

              • Topic 7


                Remember : a diode only allows electricity to flow one way. 
                Make sure you have the diode pointing the right way!
                // 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:

                Serial.println(Temperature);  // read the results in serial window

                Tutorial on INPUT_PULLUP

                Note : the temperature changes the resistance of the diode. As the temp rises, the measured resistance decreases (inverserse)
              • Topic 8

                Measuring temperature



                void setup(){

                void loop()

                int sensorValue = analogRead(A0);



                reads any sensor as a value between 0 - 512

                /*  this code for mcp9000 chip.
                it shows how to break up the sketch into various subroutines
                to make it more efficient

                from "Arduino projects to save the world"

                float temp0            // float is a real number variable
                float calibration0 = 0;
                int ADC0;
                int MCPoffset = 500;

                void setup() {
                 Serial.begin(9600); // sets up serial monitor

                void loop() {
                  getADC();         // reads analog to digital port
                  temp0 = calcTemp(ADC0,MCPoffset,calibration0);
                void getADC(){
                  ADC0 = analogRead(A0);

                // note use of parameter passing in this *function*
                float calcTemp(int val, int offset, float cal){
                return(((val*4.8828) - offset)/10) + cal;
                • Topic 9

                  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 10

                    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");

                    • Topic 11


                      • Topic 12

                        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 13

                          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 14


                            Moisture sensor
                            • Topic 15

                              • Topic 16


                                • Topic 17


                                  • Topic 18


                                    • Topic 19


                                      • Topic 20

                                        • Topic 21

                                          • Topic 22

                                            • Topic 23