Ultimate Sensor Kit (HCKITS0045)

Hobby Components modules
Post Reply
admin
Site Admin
Posts: 866
Joined: Sun Aug 05, 2012 4:02 pm

Ultimate Sensor Kit (HCKITS0045)

Post by admin » Thu Oct 22, 2015 9:32 am

Image

Our Ultimate Sensor Kit has been revamped, and, unlike most in the market, is mercury free! It still contains 37 modules however, as we have added (by popular request!) an ultrasonic module and a water sensor to the kit. We also now include a colour 78 page wiro-bound book in with the kit which contains connections diagrams, pinouts and sketches, making it even easier to jump right in. For convenience, the sketches found within the booklet are listed below also. Library links, errata and any updates are at the bottom of this post.

You can purchase our Ultimate Sensor kit and book here.

Contents of this forum is copy write Hobby Components Ltd and may not be copied or reproduced in any way without written permission. All sketches within this thread are made freely available and can be used or altered in any way you like but please leave reference to HobbyComponents.com should you decide to redistribute them.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE. HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER.



The kit contains the following modules:

Item 1: Water sensor

Code: Select all

/* Defines the DIO pin that will be used to communicate with the sensor */
#define WATERSENSOR_DIO 2

/* Initialises serial and DIO */
void setup()
{
  /* Sets up the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configures the DIO pin the sensor will be connected to as an input */
  pinMode(WATERSENSOR_DIO, INPUT); 
}


/* Main program loop */
void loop()
{

  /* If the DIO pin is pulled low (when water is detected), this prints “Water Detected” to the Serial Monitor */
  if (!digitalRead(WATERSENSOR_DIO))
    Serial.println("Water detected !");
    
Item 2: Ultrasonic module

Code: Select all

#define SOUNDCONSTANT 340.29
#define TRIG 2
#define ECHO 3

void setup() 
{
  Serial.begin(9600);
  
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  digitalWrite(TRIG, LOW);
}

void loop() 
{
  unsigned long TimeMs;
  double DistanceCM;
  
  /* Triggers the module to send a ping */
  digitalWrite(TRIG, HIGH);
  digitalWrite(TRIG, LOW);
  
  /* Measures the time for ultrasonic burst to leave and return. */
  TimeMs = pulseIn(ECHO, HIGH);
  
  /* Calculates the distance to an object in CMs */
  DistanceCM = SOUNDCONSTANT * ((double)TimeMs / 20000);
  
  Serial.print("Distance to object: ");
  Serial.print(DistanceCM);
  Serial.println(" cm");
  
  delay(1000);
}

Item 3: XY axis joystick module

Code: Select all

#define JOYS_VRX_DIO A0    /* Selects the input pin for the joysticks X-Axis */
#define JOYS_VRY_DIO A1    /* Selects the input pin for the joysticks Y-Axis */

#define JOYS_SW_DIO 2      /* Selects the input pin for the joysticks push button */


/* Initialises serial and DIO */
void setup()
{
  /* Sets up the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configures the DIO pin that the joysticks push button will be connected 
     to. As it has no pull-up we will need to enable the Arduino's internal pull-up */
  pinMode(JOYS_SW_DIO, INPUT); 
  digitalWrite(JOYS_SW_DIO, HIGH); // turns on pull-up resistors
}


/* Main program loop */
void loop()
{
  /* Reads the current position of the joysticks X & Y axis via the analogue pins */
  Serial.print("X axis: ");
  Serial.print(analogRead(JOYS_VRX_DIO));
  Serial.print("  Y axis: ");
  Serial.print(analogRead(JOYS_VRY_DIO));
  
  /* Reads the state of the push button and if pressed, outputs the state to the 
     serial port */
  if (!digitalRead(JOYS_SW_DIO))
  {
    Serial.println("  Button pressed !");
  }else
  {
     Serial.println();
  }
}

Item 4: Flame detection sensor module

Code: Select all

/* Selects the input pin for the flame detectors analogue output. */
#define FLAME_DETECT_ANA A0     
                                  
/* Selects the input pin for the flame detectors digital output. */
#define FLAME_DETC_DIO 2       
                                  


/* Initialises serial and DIO */
void setup()
{
  /* Sets up the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configures the DIO pin the sensors digital output will be connected to */
  pinMode(FLAME_DETC_DIO, INPUT); 
}


/* Main program loop */
void loop()
{
  /* Reads the sensors analogue output and sends it to the serial port */
  Serial.print("Sensor Value: ");
  Serial.print(analogRead(FLAME_DETECT_ANA));
  
  /* Reads the status of the sensors digital output and if it is high 
     then sends an alert to the UART */
  if (digitalRead(FLAME_DETC_DIO))
  {
    Serial.println(" FLAME DETECTED!");
  }else
  {
    Serial.println();
  }
    
}

Item 5: 3 colour led module

Code: Select all

#define BLUE_LED_DIO 11     /* Selects the DIO for driving the BLUE LED */
#define RED_LED_DIO 9       /* Selects the DIO for driving the RED LED */
#define GREEN_LED_DIO 10    /* Selects the DIO for driving the GREEN LED */

/* Initialises serial and DIO */
void setup()
{
  /* Configures the DIO pins used by the analogWrite PWM function  */
  pinMode(BLUE_LED_DIO, OUTPUT); 
  pinMode(RED_LED_DIO, OUTPUT); 
  pinMode(GREEN_LED_DIO, OUTPUT); 
}

/* Main program loop */
void loop()
{
  int k;
  
  /* Slowly reduces the red LED's intensity and at the same time 
     increases the green LED's intensity */
  for (k = 0; k <=255; k++)
  {
    analogWrite(RED_LED_DIO,255 - k);
    analogWrite(GREEN_LED_DIO, k);  
    delay(10);
  }
  
  /* Slowly reduces the green LED's intensity and at the same time 
     increases the blue LED's intensity */
  for (k = 0; k <=255; k++)
  {
    analogWrite(GREEN_LED_DIO,255 - k);  
    analogWrite(BLUE_LED_DIO, k);  
    delay(10);
  }
  
  /* Slowly reduces the blue LED's intensity and at the same time 
     increases the red LED's intensity */
  for (k = 0; k <=255; k++)
  {
    analogWrite(BLUE_LED_DIO,255 - k); 
    analogWrite(RED_LED_DIO, k);  
    delay(10);
  }
}

Item 6: Heartbeat sensor module

Code: Select all

/* Connects the sensors output pin 'S' to analogue pin A0 */
#define SENSOR_PIN A0
/* Pin used to control the 'L' LED */
#define LED_PIN 13
/* Detection threshold. 
   If LED is always on then lower this value.
   If LED is always off then increase this value */ 
#define THRESHOLD 2
#define SAMPLES 100

unsigned int Average[SAMPLES];
byte CurMeas = 0;

void setup()
{
  pinMode(LED_PIN, OUTPUT); // Set the pin used to control the 'L' LED to an output
  digitalWrite(LED_PIN, LOW); //Make sure the LED is off
}

void loop()
{
  /* Take a measurement */ 
  Avarage[CurMeas] = analogRead(A0) >> 3;
  
  /* Has a beat been detected? */
  if(DetectBeat(Avarage[CurMeas]))
  {
    digitalWrite(LED_PIN,HIGH); // If so then pulse the LED
    delay(100);
    digitalWrite(LED_PIN,LOW); 
  }
  
  CurMeas++; /* Move the measurement point */
  if (CurMeas >= SAMPLES)
    CurMeas = 0;
}
/* Find the maximum value from the last 100 samples */
unsigned int GetMax(void)
{
  byte index = SAMPLES;
  unsigned int Max = 0;
  
  while(index)
  {
    if(Avarage[index] > Max)
      Max = Avarage[index];
    index--; 
  }
  return Max;
}

/* Detect a beat */
boolean DetectBeat(unsigned int Measurement)
{
 if(Measurement > (GetMax() - THRESHOLD))
 {
  return  true;
 }else
 {
   return false;
 }
}

Item 7: 44E hall effect sensor module

Code: Select all

/* Select the input pin for the hall effect sensors output. */
#define HALL_SENSOR_DIO 2       
                                  


/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin which the sensors digital output will be connected to */
  pinMode(HALL_SENSOR_DIO, INPUT); 
}


/* Main program loop */
void loop()
{
  /* Read the status of the sensors digital output and if it is low 
     then send an alert to the serial port */
  if (!digitalRead(HALL_SENSOR_DIO))
  {
    Serial.println("MAGNETIC FIELD DETECTED!");
  }  
}

Item 8: 5V relay module

Code: Select all

#define PUSHBUTTON 2 //Digital pin for push button module
#define RELAY 3 //Digital pin for relay module

void setup() 
{
  pinMode(PUSHBUTTON, INPUT ); //Set pin for push button as an input
  pinMode(RELAY,OUTPUT); //Set pin to control relay as an output
  
  digitalWrite(RELAY, LOW); //Make sure the relay is de-energised
}

void loop() 
{
  if(digitalRead(PUSHBUTTON) == LOW) //Has the button been pressed?
  {
    digitalWrite(RELAY, HIGH); //If so then energise the relay
    delay(2000); //Then wait 2 seconds
    digitalWrite(RELAY, LOW); //Then de-energise the relay
  }
}

Item 9: Linear hall sensor module

Code: Select all

/* Select the input pin for the sensors analogue output. */
#define HALL_SENSOR_ANA A1     
                                  
/* Select the input pin for the sensors digital output. */
#define HALL_SENSOR_DIO 2       

/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin the sensors digital output will be connected to */
  pinMode(HALL_SENSOR, INPUT); 
}

/* Main program loop */
void loop()
{
  /* Read the sensors analogue output and send it to the serial port */
  Serial.print("Sensor Value: ");
  Serial.print(analogRead(HALL_SENSOR_ANA));
  
  /* Read the status of the sensors digital output and if it is high 
     then send an alert to the UART */
  if (digitalRead(HALL_SENSOR_DIO))
  {
    Serial.println(" MAGNET DETECTED!");
  }else
  {
    Serial.println();
  }
    
}

Item 10: 3 colour smd led module
Caution: This module does not include current limiting resistors.

Code: Select all

#define BLUE_LED_DIO 11     /* Select the DIO for driving the BLUE LED */
#define RED_LED_DIO 9       /* Select the DIO for driving the RED LED */
#define GREEN_LED_DIO 10    /* Select the DIO for driving the GREEN LED */

/* Initialise serial and DIO */
void setup()
{
  /* Configure the DIO pins used by the analogWrite PWM function  */
  pinMode(BLUE_LED_DIO, OUTPUT); 
  pinMode(RED_LED_DIO, OUTPUT); 
  pinMode(GREEN_LED_DIO, OUTPUT); 
}

/* Main program loop */
void loop()
{
  int index;
  
  /* Slowly reduce the red LED's intensity and at the same time 
     increase the green LED's intensity */
  for (index = 0; index <=255; index++)
  {
    analogWrite(RED_LED_DIO,255 - index);
    analogWrite(GREEN_LED_DIO, index);  
    delay(10);
  }
  
  /* Slowly reduce the green LED's intensity and at the same time 
     increase the blue LED's intensity */
  for (index = 0; index <=255; index++)
  {
    analogWrite(GREEN_LED_DIO,255 - index);  
    analogWrite(BLUE_LED_DIO, index);  
    delay(10);
  }
  
  /* Slowly reduce the blue LED's intensity and at the same time 
     increase the red LED's intensity */
  for (index = 0; index <=255; index++)
  {
    analogWrite(BLUE_LED_DIO,255 - index); 
    analogWrite(RED_LED_DIO, index);  
    delay(10);
  }
}

Item 11: Automatic flashing led module
This module simply flashes colours and various patterns automatically, therefore no sketch is required.

Item 12: Temperature sensor module

Code: Select all

/* This sketch requires two libraries to be downloaded and installed.
* Please visit http://hobbycomponents.com/sensorkit for library download links */

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

// Digital pin used for the temperature sensor
#define DS18PIN 2

//Create an instance of the OneWire library
OneWire oneWire(DS18PIN);

// Create an instance of the Dallas library
DallasTemperature sensors(&oneWire);

void setup(void)
{
  Serial.begin(9600);

  // Initialise the library
  sensors.begin();
}


void loop(void)
{ 
  // Send a global request for a temperature measurement
  sensors.requestTemperatures();

Serial.print("Temperature (oC): ");
  Serial.println(sensors.getTempCByIndex(0));

  //Wait a second before measuring temperature again

delay(1000);  
}
  

Item 13: Clap sensor module

Code: Select all

/* Select the input pin for the sensors analogue output. */
#define MICROPHONE_ANA_PIN A0     
                                  
/* Select the input pin for the sensors digital output. */
#define MICROPHONE_DIG_PIN 2       
                                  


/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin which the sensors digital output will be connected to */
  pinMode(MICROPHONE_DIG_PIN, INPUT); 
}


/* Main program loop */
void loop()
{
  if (digitalRead(MICROPHONE_DIG_PIN))
  {
    Serial.print("CLAP DETECTED! ");
    Serial.print("Sensor Value: ");
    Serial.println(analogRead(MICROPHONE_ANA_PIN));
  }
}

Item 14: Capacitive touch sensor module

Code: Select all

/* Select the input pin for the touch detectors analogue output. */
#define TOUCH_DETECT_ANA A0     
                                  
/* Select the input pin for the touch detectors digital output. */
#define TOUCH_DETC_DIO 2       
                                  


/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin which the sensors digital output will be connected to */
  pinMode(TOUCH_DETC_DIO, INPUT); 
}


/* Main program loop */
void loop()
{
  /* Read the sensors analogue output and send it to the serial port */
  Serial.print("Sensor Value: ");
  Serial.print(analogRead(TOUCH_DETECT_ANA));
  
  /* Read the status of the sensors digital output and if it is high 
     then send an alert to the serial port */
  if (digitalRead(TOUCH_DETC_DIO))
  {
    Serial.println(" TOUCH DETECTED!");
  }else
  {
    Serial.println();
  }    
}

Item 15: Two colour 5mm led module

Code: Select all

#define RED_LED_DIO 9       /* Select the DIO for driving the RED LED */
#define GREEN_LED_DIO 10    /* Select the DIO for driving the GREEN LED */

/* Initialise serial and DIO */
void setup()
{
  /* Configure the DIO pins used by the analogWrite PWM function  */
  pinMode(RED_LED_DIO, OUTPUT); 
  pinMode(GREEN_LED_DIO, OUTPUT); 
}

/* Main program loop */
void loop()
{
  int k;
  
  /* Slowly reduce the red LED's intensity and at the same time 
     increase the green LED's intensity */
  for (k = 0; k <=255; k++)
  {
    analogWrite(RED_LED_DIO,255 - k);
    analogWrite(GREEN_LED_DIO, k);  
    delay(10);
  }
  
  /* Slowly reduce the green LED's intensity and at the same time 
     increase the red LED's intensity */
  for (k = 0; k <=255; k++)
  {
    analogWrite(GREEN_LED_DIO,255 - k);  
    analogWrite(RED_LED_DIO, k);  
    delay(10);
  }
}

Item 16: Laser module
Please take care not to look directly into the beam as prolonged exposure may cause permanent damage to your eyes.
Receive Sketch

Code: Select all

/* Laser receive sketch */

/* Sets the threshold level. 
   If the 'L' LED stays on when the laser is not on then increase this value.
   If the 'L' doesn't light up when the laser is on then reduce this value */
#define THRESHOLD 100

/* Used to control the 'L' LED on your Arduino */
#define LED_PIN 13

/* Connect the 'S' pin of your LDR module to analogue pin A0 */
#define LDR_PIN A0 

void setup()
{
  pinMode(LED_PIN, OUTPUT); // Set the LED pin as an output
}

void loop()
{
  /* Has the laser been detected? */
  if(analogRead(LDR_PIN) < THRESHOLD)
  {
    digitalWrite(LED_PIN, HIGH); //If so then turn the 'L' LED on.
  }else
  {
    digitalWrite(LED_PIN, LOW); //If not then turn the 'L' LED off.
  }
}
Transmit Sketch

Code: Select all

/* Laser transmit sketch. */

/* Connect the 'S' pin of the laser to pin 13 on your Arduino. */
#define LASER_PIN 13 


void setup() 
{                
  pinMode(LASER_PIN, OUTPUT); //Set laser pin as an output
}

void loop() 
{
  digitalWrite(LASER_PIN, HIGH); //Turn laser on for 1 second
  delay(1000);          
  digitalWrite(LASER_PIN, LOW);  //Turn laser off for 1 second
  delay(1000); 
}

Item 17: Ball switch module

Code: Select all

/* Select the input pin for the sensors output. */
#define BALL_SENSOR_DIO 2       

/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin which the sensors digital output will be connected to */
  pinMode(BALL_SENSOR_DIO, INPUT); 
}


/* Main program loop */
void loop()
{
  /* Read the status of the sensors digital output and if it is low 
     then send an alert to the serial port */
  if (!digitalRead(BALL_SENSOR_DIO))
  {
    Serial.println("TILT DETECTED!");
  }  
}

Item 18: Analogue temperature sensor module

Code: Select all

/* Select the input pin used to measure the sensors analogue output */
#define TEMP_SENS_PIN A0        

/* Amount of averaging required */
#define AVERAGING 1024
                            
/* Used to count the number of averages made */                                  
int Index;

/* Stores the averaged result */
unsigned long Average;

/* Initialise serial */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
}


/* Main program loop */
void loop()
{
  /* Reset the average and start a new measurement */
  Average = 0;
  for (Index = 0; Index <= AVERAGING; Index++)
  {
    Average = Average + analogRead(TEMP_SENS_PIN);
  }
 Average = Average / AVERAGING;
 
  /* Send the result to the serial port */
  Serial.print("Sensor Value: ");
  Serial.println(Average);  
}

Item 19: Sound sensor module

Code: Select all

/* Select the input pin for the sensors analogue output. */
#define MICROPHONE_ANA_PIN A0     
                                  
/* Select the input pin for the sensors digital output. */
#define MICROPHONE_DIG_PIN 2       
                                  


/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin which the sensors digital output will be connected to */
  pinMode(MICROPHONE_DIG_PIN, INPUT); 
}


/* Main program loop */
void loop()
{
  if (digitalRead(MICROPHONE_DIG_PIN))
  {
    Serial.print("CLAP DETECTED! ");
    Serial.print("Sensor Value: ");
    Serial.println(analogRead(MICROPHONE_ANA_PIN));
  }
}

Item 20: Temperature alarm sensor module

Code: Select all

/* Select the input pin used to measure the sensors analogue output */
#define TEMP_SENS_ANA_PIN A0   

/* Select the input pin used to measure the sensors digital output */
#define TEMP_SENS_DIG_PIN 2 

/* Amount of averaging required */
#define AVERAGING 1024

/* Initialise serial and DIO*/
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin the sensors digital output will be connected to */
  pinMode(TEMP_SENS_DIG_PIN, INPUT);
}


/* Main program loop */
void loop()
{
  /* If the temperature goes above the threshold then output an 
     averaged reading of the sensor analogue output */
  if(digitalRead(TEMP_SENS_DIG_PIN))
  {
    Serial.print("Sensor Value: ");
    Serial.println(Av_Temp(TEMP_SENS_ANA_PIN));
  }  
}


/* This function is called by the main program and performs 
   an averaged measurement of the sensors analogue output */
int Av_Temp(byte Pin)
{
  /* Used to count the number of averages made */                                  
  int Index;
  /* Stores the averaged result */
  unsigned long Average = 0;

  /* Takes several readings of the analogue pin and average the result */
  for (Index = 0; Index <= AVERAGING; Index++)
  {
    Average = Average + analogRead(Pin);
  }
  return (Average / AVERAGING);
}

Item 21: Two colour 3mm led module

Code: Select all

#define RED_LED_DIO 9       /* Select the DIO for driving the RED LED */
#define GREEN_LED_DIO 10    /* Select the DIO for driving the GREEN LED */

/* Initialise serial and DIO */
void setup()
{
  /* Configure the DIO pins used by the analogWrite PWM function  */
  pinMode(RED_LED_DIO, OUTPUT); 
  pinMode(GREEN_LED_DIO, OUTPUT); 
}

/* Main program loop */
void loop()
{
  int k;
  
  /* Slowly reduce the red LED's intensity and at the same time 
     increase the green LED's intensity */
  for (k = 0; k <=255; k++)
  {
    analogWrite(RED_LED_DIO,255 - k);
    analogWrite(GREEN_LED_DIO, k);  
    delay(10);
  }
  
  /* Slowly reduce the green LED's intensity and at the same time 
     increase the red LED's intensity */
  for (k = 0; k <=255; k++)
  {
    analogWrite(GREEN_LED_DIO,255 - k);  
    analogWrite(RED_LED_DIO, k);  
    delay(10);
  }
}

Item 22: Push button module

Code: Select all

/* Define the DIO pin that will be connected to the sensors DOUT pin */
#define SENS_DIO 2

/* Holds the amount of times the button has been pressed */
int Counter;

/* Holds the current state of the DOUT pin */
boolean CurrentState;

/* Holds the state of the DOUT pin the last time it was read */
boolean LastState;

/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the button */
  Serial.begin(9600);
  
  /* Configure the DIO pin the module will be connected to as an input */
  pinMode(SENS_DIO, INPUT); 
  
  /* Reset the counter and last state of the DOUT pin*/
  Counter = 0;
  LastState = 0;
}


/* Main program loop */
void loop()
{
  CurrentState = digitalRead(SENS_DIO);
  /* If the button has just been pressed then add one to the counter */
  if (CurrentState && !LastState)
  {
    Counter++;
    LastState = 1;
  }
  
  /* If the button is no longer pressed then reset the last state flag */
  if(!CurrentState)
    LastState = 0;
  
  /* Output the current count to the UART */  
  Serial.print("Button Press Count: ");
  Serial.println(Counter);
    
}

Item 23: Photo resistor module

Code: Select all

/* Define the analogue pin used to read the photo resistor */

#define LDRPin 0

void setup()
 {
Serial.begin(9600);
 }

/* Main Program */

void loop()
 {
Serial.print("Photoresistor: ");
Serial.println(analogRead(LDRPin));

/* Wait 1 second and then read again */

delay(1000);
}

Item 24: IR emission module
CAUTION: This modules does not include a current limiting resistor.

Code: Select all

/* IR LED example sketch transmits IR remote codes using the standard NEC IR protocol
   IR LED Module must be connected to digital PIN 13 on Uno, Nano, or Pro Mini, or
   digital pin 11 on Mega */

boolean PinState, PulseState;
byte IRCode;

void setup()
{
  pinMode(13,OUTPUT); //Sets the IR LED pin as an output
  IRCode = 0;
}


/* Main program */
void loop()
{
  SendCode(255, IRCode); // Sends an IR code and an address of 0
   
  IRCode++; //Step to the next code
   
  delay(500); //Wait a little before sending the next code
}


/* Sends a full IR remote code */
void SendCode(byte address, byte command)
{
  Start(); //Send the start pulse
   
  SendByte(address);  //Send the address
  SendByte(~address); //Send the inverse of the address for error checking
  SendByte(command);  //Send the command
  SendByte(~command); //Send the inverse of the address for error checking
  SendBit(true);      //Send one final bit to signify the end of the code
}


/* Used by the SendCode() function above to send one byte (8 bits) of data */
void SendByte(byte data)
{
  byte index;
  
   for(index=0; index < 8; index++)
   {
    SendBit((data >> index) & 1);
   }
}


/* Used by the SendCode() and SendByte() functions above to send one bit of data */
void SendBit(boolean state)
{
  Send32KHzPulse(22);
  if(state)
  {
    delayMicroseconds(1687);
    
  }else
  {
    delayMicroseconds(530);
  }
}


/* Tells the receiver that we are about to send a code by sending a start pulse */
void Start(void)
{
  Send32KHzPulse(349);
  delayMicroseconds(4500);
}


/* Pulses the LED at 38KHz. */
void Send32KHzPulse(unsigned int pulses)
{
  while(pulses)
  {
    PORTB |= _BV(PB5); 
    delayMicroseconds(13);
    PORTB &= ~_BV(PB5);
    delayMicroseconds(13);
    pulses--;
  }
}

Item 25: Line hunting sensor module

Code: Select all

/* Define the DIO pin that will be used to communicate with the sensor */
#define LINEHUNTSENS_DIO 2

/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin the sensor will be connected to as an input */
  pinMode(LINEHUNTSENS_DIO, INPUT); 
}


/* Main program loop */
void loop()
{
  /* If the DIO pin is pulled low then an object has been detected */
  if (!digitalRead(LINEHUNTSENS_DIO))
    Serial.println("Object detected !");
    
}

Item 26: Active buzzer module

Code: Select all

/* Define digital pin used for the buzzer */
#define speakerPin 2

void setup () {
  /* Sets the speaker pin as an output*/
  pinMode (speakerPin, OUTPUT);
}
void loop () {
  /* Sets the speaker pin HIGH (turns it on)*/
  analogWrite (speakerPin, 255);
  
  /* Waits 2 seconds*/
  delay (2000);
  
  /* Sets the speaker pin LOW (turns it off)*/
  analogWrite (speakerPin, 0);
  
  /* Waits 1 second */
  delay (1000);
}

Item 27: Magnetic reed module

Code: Select all

/* Select the input pin for the sensors analogue output. */
#define REED_ANA_PIN A0     
                                  
/* Select the input pin for the sensors digital output. */
#define REED_DIG_PIN 2       
                                  


/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin which the sensors digital output will be connected to */
  pinMode(REED_DIG_PIN, INPUT); 
}


/* Main program loop */
void loop()
{
  if (digitalRead(REED_DIG_PIN))
  {
    Serial.print("MAGNETIC FIELD DETECTED! ");
    Serial.print("Magnetic Value: ");
    Serial.println(analogRead(REED_ANA_PIN));
  }
}

Item 28: Vibration sensor module

Code: Select all

/* Select the input pin for the vibration sensors output. */
#define VIBRATION_SENSOR_DIO 2       
                                  


/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin which the sensors digital output will be connected to */
  pinMode(VIBRATION_SENSOR_DIO, INPUT); 
}


/* Main program loop */
void loop()
{
  /* Read the status of the sensors digital output and if it is low 
     then send an alert to the serial port */
  if (!digitalRead(VIBRATION_SENSOR_DIO))
  {
    Serial.println("VIBRATION DETECTED!");
  }  
}

Item 29: Temperature and humidity sensor module

Code: Select all

/* Please visit http://hobbycomponents.com/sensorkit for download links 
* for the required library */

#include <dht11.h>
 
dht11 DHT11;
 
/* Define the DIO pin that will be used to communicate with the sensor */
#define DHT11_DIO 2
 
 
void setup()
{
  /* Setup the serial port for displaying the output of the sensor */
  Serial.begin(9600);
}
 
/* Main program loop */
void loop()
{
  /* Perform a read of the sensor and check if data was read OK */
  if (DHT11.read(DHT11_DIO) == DHTLIB_OK)
  {
    /* If so then output the current temperature and humidity to 
    the serial port */
    Serial.print("Temperature: ");
    Serial.print((float)DHT11.temperature, 2);
    Serial.print("oC\t");
    Serial.print("Humidity: ");
    Serial.print((float)DHT11.humidity, 2);
    Serial.println("%");
  }else
  {
    /* If there was a problem reading from then sensor then output 
    an error */
    Serial.println("ERROR");
  }
   
  delay(500);
}

Item 30: IR sensor receiver module
Please make sure that you connect the power supply in the correct order as a reverse polarity will cause permanent damage.

Code: Select all

/* This example sketch uses the IR receiver module to decode a remote control 
   compatible with the NEC protocol */


/* Connect the OUT pin (labelled Y on some modules) to digital pin 13 */
#define RECIVER_PIN 13 

unsigned long StartTime, EndTime;
byte Index, Address, InvAddress, Command, InvCommand;


void setup()
{
  Serial.begin(9600); //Initialise the serial port.
  pinMode(RECIVER_PIN, INPUT); // Set receiver pin as an input.
}


void loop()
{
  /* Check if a button has been pressed */
  if(CheckStartPulse())
  {
    Address = GetData();    //Get the buttons address.
    InvAddress = GetData(); //Get the inverse of the address
    Command = GetData();    //Get the command code
    InvCommand = GetData(); //Get the inverse of the command code
   
    /* Output the results to the serial port */
    Serial.print("Address: ");
    Serial.print(Address, HEX);
    Serial.print(" ");
    Serial.print("Code: ");
    Serial.print(Command, HEX);
    Serial.print(" ");
    if((Address + InvAddress) - (Command + InvCommand)) //Should equal zero!
      Serial.print("ERROR!");
      
    Serial.println("");
  }
}


/* Checks for a 9ms pulse followed by a 4.5ms space */
boolean CheckStartPulse(void)
{
  if(digitalRead(RECIVER_PIN) == LOW)
  {
    StartTime = millis();
    EndTime = StartTime;
    
    /* Check for pulse */
    while((EndTime - StartTime) <= 30 && digitalRead(RECIVER_PIN) == LOW)
    {
      EndTime = millis();
    }
    
    /* If pulse is valid check for space */
    if((EndTime - StartTime) >= 8 && (EndTime - StartTime) <= 10)
    {
    
      while((EndTime - StartTime) <= 30 && digitalRead(RECIVER_PIN) == HIGH)
      {
        EndTime = millis();
      }
    }
    
    if((EndTime - StartTime) >= 13 && (EndTime - StartTime) <= 16)
     return true;
  }
  return false;
}


/* Get one bit of data */
byte GetBit(void)
{
 while(digitalRead(RECIVER_PIN) == LOW);
 
 StartTime = micros();
 
 while(digitalRead(RECIVER_PIN) == HIGH);
 
 EndTime = micros();

 if(EndTime - StartTime > 700)
   return 1;
   
 return 0;
  
}


/* Get one byte (8 bits) of data */
byte GetData(void)
{
  byte Data = 0;
     for(Index = 0; Index < 8; Index++)
   {
     Data = (Data << 1) + GetBit();
   }
   return Data;
}

Item 31: Obstacle avoidance sensor module
Please note: Do not attempt to drive the enable pin whilst the enable jumper is in place.

Code: Select all

/* Define the DIO pin that will be used to communicate with the sensor */
#define SENS_DIO 2

/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin the sensor will be connected to as an input */
  pinMode(SENS_DIO, INPUT); 
}


/* Main program loop */
void loop()
{
  /* If the DIO pin is pulled low then an object has been detected */
  if (!digitalRead(SENS_DIO))
    Serial.println("Object detected !");
    
}

Item 32: Passive buzzer module
Please note: Do not connect this module directly to your Arduino as it will cause permanent damage to both the module and the Arduino.

Code: Select all

#define SPEAKERPIN 2

/* Frequencies for musical scale notes */
#define NoteC1 262
#define NoteD1 294
#define NoteE1 330
#define NoteF1 349
#define NoteG1 392
#define NoteA1 440
#define NoteB1 494
#define NoteC2 523

/* Store a set of notes to play */
static int Song[] =
{NoteC1,NoteD1,NoteE1,NoteF1,NoteG1,NoteA1,NoteB1,NoteC2,NoteC2};

/* Indexes the next note to play */
byte Index;
void setup()
 {
/* Set the DIO pin that the speaker is connected to as an output and then set it
low so that it isn't driving the speaker */
pinMode(SPEAKERPIN, OUTPUT);
digitalWrite(SPEAKERPIN, LOW);
 }
 
/* Main program */
void loop()
 {
  
/* Play each note in sequence */
for(Index = 0; Index < 9; Index++)
 {
/* Output the current note to the speaker for 500ms*/
tone(SPEAKERPIN, Song[Index]) ;
delay(500);
 }
 
/* Stop playing the last note */
noTone(SPEAKERPIN);

/* Wait a second before starting again */
delay(1000);
}

Item 33: Mini magnetic reed module

Code: Select all

/* Select the input pin for the sensors digital output. */
#define REED_DIG_PIN 2       
                                  


/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin which the sensors digital output will be connected to */
  pinMode(REED_DIG_PIN, INPUT); 
}


/* Main program loop */
void loop()
{
  if (!digitalRead(REED_DIG_PIN))
  {
    Serial.print("MAGNETIC FIELD DETECTED! ");
  }
}

Item 34: Rotary encoder module

Code: Select all

/* Define digital pins used to read the encoder */
#define DT 2
#define CLK 3
#define SW 4
 
 
void setup()
{
  Serial.begin(9600); 
  pinMode(DT, INPUT);   
  pinMode(CLK, INPUT);  
  pinMode(SW, INPUT);  
  digitalWrite(SW, HIGH);
}
 
/* Main program */
void loop()
{
 int counter;
 byte DialPos;
 byte Last_DialPos;
  
 /* Reset the counter */
 counter = 0;
  
  /* Continuously read the state of the encoder */
  while(1)
  {
    /* Read the status of the dial */
    DialPos = (digitalRead(CLK) << 1) | digitalRead(DT);
   
    /* Is the dial being turned anti-clockwise? */
    if (DialPos == 3 && Last_DialPos == 1)
    {
      counter--;
    }
   
    /* Is the dial being turned clockwise? */
    if (DialPos == 3 && Last_DialPos == 2)
    {
      counter++;
    }
   
    /* Output the counter to the serial port */
    Serial.println(counter);
  
    /* Is the switch pressed? */
    if(!digitalRead(SW))
      Serial.println("Switch pressed!");
   
    /* Save the state of the encoder */
    Last_DialPos = DialPos;
  }
}

Item 35: 504BG analogue hall sensor module

Code: Select all

/* Define the analogue pin used to read the sensor */

#define SENS_PIN A0

void setup()
{
  Serial.begin(9600);
}

/* Main Program */

void loop()
{
  unsigned int Sensor;
  
  Sensor = analogRead(SENS_PIN);
  Serial.print("Hall sensor: ");
  Serial.print(analogRead);

  if(Sensor < 300)
  {
    Serial.println(" - South poll detected.");
  }else if(Sensor > 700)
  {
    Serial.println(" - North poll detected.");
  }else
  {
     Serial.print("”);
  }

  /* Wait 1 second and then read again */
  delay(1000);
}

Item 36: Impact sensor module

Code: Select all

/* Select the input pin for the impact sensors output. */
#define IMPACT_SENSOR_DIO 2 

/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin which the sensors digital output will be connected to */
  pinMode(IMPACT_SENSOR_DIO, INPUT); 
}


/* Main program loop */
void loop()
{
  /* Read the status of the sensors digital output and if it is low 
     then send an alert to the serial port */
  if (!digitalRead(IMPACT_SENSOR_DIO))
  {
    Serial.println("IMPACT DETECTED!");
  }  
}

Item 37: Optical break module

Code: Select all

/* Define the DIO pin that will be connected to the sensors DOUT pin */
#define SENS_DIO 2

/* Holds the amount of times the IR beam has been broken */
int Counter;

/* Holds the current state of the DOUT pin */
boolean CurrentState;

/* Holds the state of the DOUT pin the last time it was read */
boolean LastState;

/* Initialise serial and DIO */
void setup()
{
  /* Setup the serial port for displaying the status of the sensor */
  Serial.begin(9600);
  
  /* Configure the DIO pin the sensor will be connected to as an input */
  pinMode(SENS_DIO, INPUT); 
  
  /* Reset the counter and last state of the DOUT pin*/
  Counter = 0;
  LastState = 0;
}


/* Main program loop */
void loop()
{
  CurrentState = digitalRead(SENS_DIO);
  /* If the beam has just been broken then add one to the counter */
  if (CurrentState && !LastState)
  {
    Counter++;
    LastState = 1;
  }
  
  /* If the beam is no longer broken then reset the last state flag */
  if(!CurrentState)
    LastState = 0;
  
  /* Output the current count to the UART */  
  Serial.print("Trigger count: ");
  Serial.println(Counter);
    
}
Libraries

Library for Dallas DS18B20 temperature sensor can be downloaded from here:
http://www.milesburton.com/?title=Dalla ... ol_Library

You will also need a copy of the OneWire library. More information about this library can be found on the Arduino website here:
http://playground.arduino.cc/Learning/OneWire

You can also download a known working snapshot of these two libraries below (please log in to download):
DallasTemperature.zip
OneWire.zip
These libraries are distributed under the GNU Lesser General Public License (2.1) which can be found here:

http://www.gnu.org/licenses/old-license ... .1.en.html

Library for DHT11 temperature/humidity sensor module can be found on the Arduino website here:
http://playground.arduino.cc/main/DHT11Lib

You can also download a snapshot of this library below (please log in to download):
DHT11.zip
Theis libraries is distributed under the GNU Lesser General Public License (3.0) which can be found here:

http://www.gnu.org/licenses/lgpl-3.0.en.html



Erratta
You do not have the required permissions to view the files attached to this post.

Post Reply

Return to “Modules”