Intermediate Arduino

From XinCheJian
Jump to navigation Jump to search

These are some ideas for an Intermediate Arduino class.

Basically, this is finishing up using all of the components that are included in the Arduino Beginner Kit from DF Robot.

Summary

Requirements

Overview Run Time: 2 Hours

  • Relay
  • Tilt Switch Sensor
  • IR Remote Control
  • Ultrasonic Sensor (no longer included in kit)



Relay

You'll need the following items:

  • Relay

A relay is an electrically operated switch. Many relays use an electromagnet to mechanically operate a switch, but other operating principles are also used, such as solid-state relays. Relays are used where it is necessary to control a circuit by a low-power signal (with complete electrical isolation between control and controlled circuits), or where several circuits must be controlled by one signal.

Tilt Switch

This is taken directly from DF Robot's website.

Lesson 5 B.png

You'll need the following items:

  • Tilt Switch
  • Breadboard

Explanation

int light=8;//Connect the led to Digital Pin 8 

void setup()
{
    pinMode(light,OUTPUT);//C
}
void loop()
{
    int i;
    while(1)
    {
       i=analogRead(5);//Read Analog Pin 5 which connect to Tilt Sensor
       if(i>200)// if reading is over 200(1V) (When you tilt the tilt sensor)
       {
          digitalWrite(light,HIGH);//Turn on led
       }
       else
       {
          digitalWrite(light,LOW);//Turn off led
       }
    }
}

IR Remote Control

350px-IRKit1.jpg 450px-14 Infrared controlled Light.png 450px-14 Infrared controlled Light 1.png

IR is widely used in remoter control. With this IR receiver, the Arduino project is able to receive command from any IR remoter controller if you have the right decoder. Well, it will be also easy to make your own IR controller using IR transmitter.


You'll need the following items:

  • IR Remote Control
  • IR Receiver
/*
     Infrared controlled Light 
*/
#include <IRremote.h>           
int RECV_PIN = 11;                
int ledPin = 10;                
boolean ledState = LOW;        
IRrecv irrecv(RECV_PIN);      
decode_results results;        
 
void setup(){
  Serial.begin(9600);         
  irrecv.enableIRIn();         
  pinMode(ledPin,OUTPUT);     
}
 
void loop() {
  if (irrecv.decode(&results)) {  
      Serial.println(results.value, HEX);
     
      if(results.value == 0xFD00FF){
               ledState = !ledState;            
               digitalWrite(ledPin,ledState); 
           }     
   irrecv.resume();  
   }
}

Ultrasonic Sensor

Arduino-example-ultrasonic.png

What you need:

  • Breadboard
  • Ultrasonic sensor


Open the following example: Arduino > Examples > Sensors > PING


// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

We'll need to modify slightly to the following since we have one pine to tx (send) and one pin to rx (receive):

/* spot the two changes */

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7; // transmission pin
const int pingRx = 8; //receiving pin

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(pingPin, OUTPUT);
  pinMode(pingRx, INPUT);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  duration = pulseIn(pingRx, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(100);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Segment Display



Other Function Ideas

  • Serial communication?
  • Ultrasonic Range Finder [1]
  • Accelerometer [2]
  • Computer to Arduino Control - Dimmer [3]
  • Calibration [4]
  • Internet??? Anyway to get connected to the cloud?



Challenges

Simple Keyboard


See also