Category Archives: Arduino

Controlling an LED light using a mobile app and Bluetooth

This tutorial will show you how to control an LED light on an Arduino using a mobile app and a Bluetooth wireless connection.

Parts required

Here is what you’ll need:

  • An Android phone or iPhone
  • A free mobile app
  • Arduino Uno board
  • Breadboard
  • 220 Ohm resistor
  • 6 male-to-male jumper wires
  • HM-10 BLE Bluetooth 4.0 module (see bottom of page for exact model used in this tutorial)
  • An LED light
  • Arduino IDE software

Wiring it up

Wire up the project using jumper wires by following these instructions or checking out the circuit diagram and images below.

  1. Connect VCC on Bluetooth module to 3.3V on Arduino
  2. Connect GND on Bluetooth module to GND on Arduino
  3. Connect TXD on Bluetooth module to digital pin 7 on Arduino
  4. Connect RXD on Bluetooth module to digital pin 8 on Arduino
  5. Connect the long pin from the LED on the breadboard to digital pin 2 on Arduino
  6. Connect the short pin from the LED on the breadboard to a 220 Ohm resistor and from there to GND on the Arduino

Note: The STATE and EN pins on the Bluetooth module do not need to be connected to anything in this project.

Bluetooth

Here are some images showing the LED turned on and everything wired up.

IMG_1905

IMG_6092

IMG_5111

Arduino code

Create a new sketch in the Arduino IDE and add the following code. Then upload the sketch to your Arduino board.

#include <SoftwareSerial.h>
int led = 2;

SoftwareSerial Bluetooth(7, 8);

void setup() {  
  Serial.begin(9600);
  Bluetooth.begin(9600);
  pinMode(led, OUTPUT);
}

void loop() {  
  int command;
  
  if (Bluetooth.available()) {
    command = Bluetooth.read();  
    Serial.println("Input received:");
    if (command != 0)
    {
      // A non-zero input will turn on the LED
      Serial.println("0 / ON");
      digitalWrite(led, HIGH);
    }
    else
    {
      // A zero value input will turn off the LED
      Serial.println("1 / OFF");
      digitalWrite(led, LOW);
    }  
  }
}

Controlling the Arduino via Bluetooth

To connect your Android or iOS mobile device to the Arduino you will need to download a free app (or you could also make your own app) that can connect to the HM-10 Bluetooth module and send commands. You can search your device’s app store to find a free app that will work for this project but for this example we will use the free Bluetooth Terminal app on iOS.

Tip: If you search the iOS App Store or Google Play app store using the keywords “bluetooth arduino” or “bluetooth hm-10” you should be able to find a suitable app.

The steps below are for the Bluetooth Terminal app on iOS but will be similar for other apps on other devices:

  1. Open the app and connect to the Bluetooth module (in this case it is called CC41-A but the name will vary depending on the model you have).
    IMG_4901
  2. Tap ‘Select characteristic’ and then select FFE1 (at the bottom of the list).
    IMG_4902
  3. Select Decimal.
  4. Using the on-screen keypad, you can enter different decimal values. Enter 1 and tap the Send button. The LED light should turn on. Enter 0 and tap the Send button. The LED should then turn off. If you don’t see anything happen, click Tools > Serial Monitor in the Arduino IDE and then set the baud date to 9600 bauds. Check if any input is being received and displayed in the serial monitor when you send the 0 or 1 commands to the Arduino from the app.
    IMG_4903

Where to from here?

You can use Bluetooth to control other projects such as a robot or car. You can also try receiving data from the Arduino – eg. send sensor readings to your phone.

Which Bluetooth module should I use?

There are several different Bluetooth modules that should work with this tutorial. The one used in this project was purchased on eBay and is called “HM-10 CC2540 CC2541 BLE Bluetooth 4.0 Wireless Module“.

Building a 4WD Autonomous Car with Arduino

In this tutorial you will learn to make a 4WD robotic car with built-in obstacle avoidance. This project involves building a 4WD car with an ultrasonic sensor that can detect nearby objects and change its direction to avoid these objects. The ultrasonic sensor will be attached to a servo motor which is constantly scanning left and right looking for objects in its way.

4WD autonomous robotic car
4WD autonomous robotic car

You can extend this project by adding more sensors such as ultrasonic sensor to the rear so that the car doesn’t reverse into objects, speed detection sensors to determine speed, line following sensors, and LED brake lights. You could even add light sensors and LEDs to turn headlights on in low-light conditions or a Bluetooth connection to control the car through a mobile app.

Parts required

Here is what you will need:

  • 1x Arduino Uno board
  • 1x 4WD Robotic Car kit (4 wheels, 4 motors, chassis, AA battery enclosure, screws)
  • 6x AA batteries
  • 1x 9V battery
  • 1x 9V battery power cable barrel jack connector
  • 1x L298N Motor Module
  • 1x Arduino Sensor Shield v5.0
  • 1x HC-SR04 Ultrasonic Sensor
  • 1x Servo motor (any small servo motor will do but if you don’t have one you can leave it out)
  • Assorted colour wires:
    • 4x female-to-female wires for Ultrasonic Sensor
    • 8x female-to-female wires for the Motor Module to Sensor Shield
    • 8x bare-ended wires to go from the four motors to the Motor Module
    • 2x bare-ended wires to go from the Motor Module to Sensor Shield
    • 3x wires for the servo motor with female end attaching to Sensor Shield (should already be included with the servo motor)
    • 2x bare-ended wires to go from AA Battery enclosure to the motor Module (should already be included with the battery enclosure)
  • Arduino IDE software – free at http://www.arduino.cc
  • 1x USB cable (A male plug to B male plug)
  • A computer to program with

Note: Make sure you get a range of different colours for the wires (eg. red for voltage, black for ground, etc.). Some items such as the battery enclosure, motors and servo motor may come with wires so you may not need to purchase these separately. Some motor modules or sensor shields may use different pins (eg. male or female or just bare-ended wires).

Below are images of the main components you need (apart from the wiring and batteries) – the different robot parts, shields, modules and sensors.

Click for larger image
Click for larger image

Assembling the robotic car

The first thing to do is to assemble all the car chassis parts and attach the motors to the wheels. This is explained in detail in the video below.

Wiring it up

Now that the chassis is assembled and the wheels and motors are attached, you will need to wire up all of the components. You can watch the video below or scroll down for the step-by-step instructions and wiring diagram.

  1. Connect the red wire (voltage) from the battery enclosure to VCC on the L298N Motor Module.
  2. Connect the black wire (ground) from the battery enclosure to GND on the Motor Module.
  3. Connect the red wire from Motor A1 (rear left) and the red wire from Motor A2 (front left) to OUT1 on the Motor Module (both wires can be twisted together and will go into OUT1 – the wiring diagram further down this page shows how to do this).
  4. Connect the black wire from Motor A1 (rear left) and the black wire from Motor A2 (front left) to OUT2 on the Motor Module.
  5. Connect the red wire from Motor B1 (rear right) and the red wire from Motor B2 (front right) to OUT3 on the Motor Module.
  6. Connect the black wire from Motor B1 (rear right) and the black wire from Motor B2 (front right) to OUT4 on the Motor Module.
  7. Connect a female-to-female end wire from ENA (Engine A) on the Motor Module to signal pin 1 on the Sensor Shield (the #2 pin in the S row. There are three rows – S stands for Signal, V for Voltage and G for Ground).
  8. Connect a female-to-female end wire from 5V (next to ENA) on the Motor Module to voltage pin 1 on the Sensor Shield (it doesn’t really matter which pin for voltage as long as it is in the V row).
  9. Connect a female-to-female end wire from IN1 on the Motor Module to signal pin 2 on the Sensor Shield.
  10. Connect a female-to-female end wire from IN2 on the Motor Module to signal pin 3 on the Sensor Shield.
  11. Connect a female-to-female end wire from IN3 on the Motor Module to signal pin 4 on the Sensor Shield.
  12. Connect a female-to-female end wire from IN4 on the Motor Module to signal pin 5 on the Sensor Shield.
  13. Connect a female-to-female end wire from 5V (next to ENB) on the Motor Module to voltage pin 6 on the Sensor Shield (it doesn’t really matter which pin for voltage as long as it is in the V row).
  14. Connect a female-to-female end wire from ENB (Engine B) on the Motor Module to signal pin 6 on the Sensor Shield.
  15. Connect the signal wire from the servo motor to signal (S) pin 7 on the Sensor Shield, the voltage wire from the servo motor to the voltage (V) pin 7 on the Sensor Shield, and the ground wire from the servo motor to the GND (G) pin 7 on the Sensor Shield.
  16. Attach the Ultrasonic Sensor to the servo motor and then attach the servo motor to the front of the car so that it can scan left and right looking for objects in its path.
    You can use sticky tape, blu-tack, glue or a 3D printed/laser cut mount or whatever you like to attach everything, as long as everything is firmly mounted.
  17. Use female-to-female end plugs to connect the Ultrasonic sensor to the Sensor Shield. Firstly, attach the TRIG pin from the Ultrasonic sensor to signal (S) pin 8 on the Sensor Shield. Attach the ECHO pin from the ultrasonic sensor to signal (S) pin 9 on the Sensor Shield. Attach the VCC pin from the Ultrasonic Sensor to the voltage (V) pin 10 on the Sensor Shield. Lastly, attach the GND pin from the Ultrasonic Sensor to the GND (G) pin 11 on the Sensor Shield.
  18. Now attach the Sensor Shield on top of the Arduino Uno board making sure to not bend any pins. Connect a 9V Battery Barrel Jack Connector to a 9V battery and then plug this in to the Arduino’s barrel jack (see diagram below).
  19. That’s it! Now all you need is batteries and the code!

Please note: There are several variations of the sensors and modules and several different ways to wire this up. For example, you can add a charging component to charge batteries and you can also add different switches. There may be other better methods to use, but I have gone for the simplest way I know of to set this up.

Wiring diagram

Click for larger image
Click for larger image

Writing the code

Here is the code to get your robot moving. The code is written in C language and is written in the Arduino IDE software. The code for a robot with a servo motor and the code for a robot without a servo motor is shown below. If you don’t have an ultrasonic sensor and just want to use Bluetooth control via a phone app, then you can just extract the parts of the code that you need and then add the Bluetooth code.

Code to use with a servo motor

// Setup the servo motor
#include <Servo.h>
Servo myservo;
int servposnum = 0;
int servpos = 0;

// Setup Motor A (front and rear) pins
int enableA = 1;
int pinA1 = 3;
int pinA2 = 2;

// Setup Motor B (front and rear) pins
int enableB = 6;
int pinB1 = 5;
int pinB2 = 4;

// Setup Ultrasonic Sensor pins
#define trigPin 8
#define echoPin 9


void setup() {
  // The setup code goes here and runs once only
  // Configure the pin modes for each drive motor
   pinMode (enableA, OUTPUT);
   pinMode (pinA1, OUTPUT);
   pinMode (pinA2, OUTPUT);

   pinMode (enableB, OUTPUT);
   pinMode (pinB1, OUTPUT);
   pinMode (pinB2, OUTPUT); 

   // Configure the pin modes for the Ultrasonic Sensor
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);

   // Turn pin into servo driver. Calls pinMode. Returns 0 on failure.
   myservo.attach(7);
}

void loop() {
  // Main code goes here and will run repeatedly:

     car(); // function keeps moving car forward while distance > 15cm
     avoid(); // function makes car go back, turn slightly right to move forward in new direction
     
}

// Create motor functions
void motorAforward() {
 digitalWrite (pinA1, HIGH);
 digitalWrite (pinA2, LOW);
}
void motorBforward() {
 digitalWrite (pinB1, LOW);
 digitalWrite (pinB2, HIGH);
}
void motorAbackward() {
 digitalWrite (pinA1, LOW);
 digitalWrite (pinA2, HIGH);
}
void motorBbackward() {
 digitalWrite (pinB1, HIGH);
 digitalWrite (pinB2, LOW);
}
void motorAstop() {
 digitalWrite (pinA1, HIGH);
 digitalWrite (pinA2, HIGH);
}
void motorBstop() {
 digitalWrite (pinB1, HIGH);
 digitalWrite (pinB2, HIGH);
}
void motorAcoast() {
 digitalWrite (pinA1, LOW);
 digitalWrite (pinA2, LOW);
}
void motorBcoast() {
 digitalWrite (pinB1, LOW);
 digitalWrite (pinB2, LOW);
}
void motorAon() {
 digitalWrite (enableA, HIGH);
}
void motorBon() {
 digitalWrite (enableB, HIGH);
}
void motorAoff() {
 digitalWrite (enableA, LOW);
}
void motorBoff() {
 digitalWrite (enableB, LOW);
}

// Setup movement functions
void forward (int duration) {
 motorAforward();
 motorBforward();
 delay (duration);
}
void backward (int duration) {
 motorAbackward();
 motorBbackward();
 delay (duration);
}
void right (int duration) {
 motorAbackward();
 motorBforward();
 delay (duration);
}
void left (int duration) {
 motorAforward();
 motorBbackward();
 delay (duration);
}
void coast (int duration) {
 motorAcoast();
 motorBcoast();
 delay (duration);
}
void breakRobot (int duration) {
 motorAstop();
 motorBstop();
 delay (duration);
}
void disableMotors() {
 motorAoff();
 motorBoff();
}
void enableMotors() {
 motorAon();
 motorBon();
}

// Setup Ultrasonic Sensor distance measuring
int distance() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  return distance;
}

// Setup the main car function
void car() {
int distance_0;
distance_0 = distance();
  // Keep moving forward in a straight line while distance of objects > 15cm
  while(distance_0 > 15)
  {
     // Keep moving servo motor back and forth to scan surroundings
     // This allows the ultrasonic sensor to see more to its left and right
     if(servposnum == 0)
     {
      myservo.writeMicroseconds (1900);
      servposnum = 1;
      delay(100);     
     }
     else if(servposnum == 1)
     {
      myservo.writeMicroseconds (2200);
      servposnum = 2;
     delay(100);
     }
     else if(servposnum  == 2)
     {
      myservo.writeMicroseconds (1900);
      servposnum = 3;
      delay(100);
     }
     else if(servposnum == 3)
     {
      myservo.writeMicroseconds (1600);
      servposnum = 1;
      delay(100);
     }
     motorAon();
     motorBon();
     forward(1);    
     distance_0 = distance();
     
  }
  breakRobot(0);

}
void avoid()
{
    // Go back and turn slightly right to move car in new direction if object detected < 15cm away
    backward(500);
    right(360);
}

Code to use without a servo motor

// Setup Motor A (front and rear) pins
int enableA = 1;
int pinA1 = 3;
int pinA2 = 2;

// Setup Motor B (front and rear) pins
int enableB = 6;
int pinB1 = 5;
int pinB2 = 4;

// Setup Ultrasonic Sensor pins
#define trigPin 8
#define echoPin 9

void setup() {
  // The setup code goes here and runs once only
  // Configure the pin modes for each drive motor
   pinMode (enableA, OUTPUT);
   pinMode (pinA1, OUTPUT);
   pinMode (pinA2, OUTPUT);

   pinMode (enableB, OUTPUT);
   pinMode (pinB1, OUTPUT);
   pinMode (pinB2, OUTPUT);

   // Configure the pin modes for the Ultrasonic Sensor
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
}

void loop() {
  // Main code goes here and will run repeatedly:
  car(); // function keeps moving car forward while distance of objects in front are > 15cm away
  avoid(); // function makes car go back, turn slightly right to move forward in new direction
}

// Create motor functions
void motorAforward() {
 digitalWrite (pinA1, HIGH);
 digitalWrite (pinA2, LOW);
}
void motorBforward() {
 digitalWrite (pinB1, LOW);
 digitalWrite (pinB2, HIGH);
}
void motorAbackward() {
 digitalWrite (pinA1, LOW);
 digitalWrite (pinA2, HIGH);
}
void motorBbackward() {
 digitalWrite (pinB1, HIGH);
 digitalWrite (pinB2, LOW);
}
void motorAstop() {
 digitalWrite (pinA1, HIGH);
 digitalWrite (pinA2, HIGH);
}
void motorBstop() {
 digitalWrite (pinB1, HIGH);
 digitalWrite (pinB2, HIGH);
}
void motorAcoast() {
 digitalWrite (pinA1, LOW);
 digitalWrite (pinA2, LOW);
}
void motorBcoast() {
 digitalWrite (pinB1, LOW);
 digitalWrite (pinB2, LOW);
}
void motorAon() {
 digitalWrite (enableA, HIGH);
}
void motorBon() {
 digitalWrite (enableB, HIGH);
}
void motorAoff() {
 digitalWrite (enableA, LOW);
}
void motorBoff() {
 digitalWrite (enableB, LOW);
}

// Setup movement functions
void forward (int duration) {
 motorAforward();
 motorBforward();
 delay (duration);
}
void backward (int duration) {
 motorAbackward();
 motorBbackward();
 delay (duration);
}
void right (int duration) {
 motorAbackward();
 motorBforward();
 delay (duration);
}
void left (int duration) {
 motorAforward();
 motorBbackward();
 delay (duration);
}
void coast (int duration) {
 motorAcoast();
 motorBcoast();
 delay (duration);
}
void breakRobot (int duration) {
 motorAstop();
 motorBstop();
 delay (duration);
}
void disableMotors() {
 motorAoff();
 motorBoff();
}
void enableMotors() {
 motorAon();
 motorBon();
}

// Setup Ultrasonic Sensor distance measuring
int distance() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  return distance;
}

// Setup the main car function
void car() {
int distance_0;
distance_0 = distance();
  // Keep moving forward in a straight line while distance of objects in front > 15cm away
  while(distance_0 > 15)
  {
     motorAon();
     motorBon();
     forward(1);
     distance_0 = distance();
  }
  breakRobot(0);

}

// Go back and turn slightly right to move car in new direction
// This function only runs if an obstacle within 15cm is detected
void avoid()
{
    backward(500);
    right(360);
}

Finishing up

Once you have the code you will need to verify it and then upload it to your Arduino Uno board. Make sure you have either the Arduino disconnected from the Sensor Shield or all batteries disconnected when uploading the code to prevent any errors while uploading. Make sure to also arrange the different components carefully in the chassis to prevent any short circuits.

And that’s it! There is so much more that can be done with this project. Try extending your project by adding the following features:

  • Line following
  • Low-light activated headlights (using light sensors and LEDs)
  • Brake lights
  • Speed measuring
  • Bluetooth/mobile app control
  • More ultrasonic sensors to avoid reversing into objects or falling off edges

The video below shows a demo of the 4WD autonomous car using an Arduino and the ultrasonic sensor attache to a servo motor. Six AA batteries power the motors while the Arduino board is powered using a separate 9V battery.

More photos

Building a 2WD Arduino robotic car

This tutorial will show you how to use an Arduino to build a 2WD robotic car. You can extend this project by using sensors to measure wheel speed (for improved accuracy when steering or maintaining a straight line) or sensors to avoid objects or follow a line.

Parts required

Here is what you will need:

  • Arduino Uno board
  • 2WD robotic car kit (2 wheels, 2 motors, chassis)
  • 4 x AA batteries
  • Motor shield with 6 wires
  • Arduino IDE software (free at http://www.arduino.cc/)
  • USB cable
  • 2 x wheel speed encoders and infrared sensors (optional)

car1 car2 Assembling the robot

robotassembly

 

  1. Assemble all chassis parts and attach the motors to the wheels (if not already assembled).
  2. Connect the red wire from the battery enclosure to VIN on the Motor Shield.
  3. Connect the black wire from the battery enclosure to GND on the Motor Shield.
  4. Connect the red wire from Motor A to A+ on the Motor Shield, and the black wire from Motor A to A- on the Motor Shield.
  5. Connect the red wire from Motor B to B+ on the Motor Shield, and the black wire from Motor B to B- on the Motor Shield.
  6. Attach the Motor Shield to the top of the Arduino Uno board – be careful not to bend or damage any of the pins!
  7. Connect the Arduino Uno board to the computer via the USB cable and upload the code using the Arduino IDE software.
  8. Insert four AA batteries in the battery enclosure.
  9. Test your robot! If your robot goes in the wrong direction or doesn’t move, check that you have the Motor pins in the correct places and make sure no pins are loose.
Extra challenges:
  • Control your robotic car using an Android app on a smartphone via Bluetooth (refer to the Controlling an LED by Bluetooth tutorial for instructions on how to make an app using App Inventor and how to attach a Bluetooth module).
  • Add code that will control the speed of the motors using the encoder discs and infrared sensors.
The code to test your robot

Use this code to test that your wheels spin and that your robot is wired up correctly.

void setup() {
    //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin
  //Setup Channel B
  pinMode(13, OUTPUT); //Initiates Motor Channel B pin
  pinMode(8, OUTPUT);  //Initiates Brake Channel B pin
  }
void loop(){
  //Motor A forward @ full speed
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins the motor on Channel A at full speed
  //Motor B backward @ half speed
  digitalWrite(13, LOW);  //Establishes backward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 123);    //Spins the motor on Channel B at half speed
  delay(3000);
  digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(8, HIGH);  //Engage the Brake for Channel B
  delay(1000);
  //Motor A forward @ full speed
  digitalWrite(12, LOW);  //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 123);    //Spins the motor on Channel A at half speed
  //Motor B forward @ full speed
  digitalWrite(13, HIGH); //Establishes forward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 255);   //Spins the motor on Channel B at full speed
  delay(3000);
  digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(8, HIGH);  //Engage the Brake for Channel B
  delay(1000);
}

Click here if you would like a PDF copy of these instructions.

Arduino weather station project

This tutorial will show you how to take temperature readings using the Freetronics temperature sensor and an Arduino Uno.

Note that there are two different module types: the temperature sensor module and the humidity & temperature sensor module. If you have the humidity & temperature sensor module you will need to download a library for the Arduino IDE and use different wiring and code – follow the instructions for that module here: http://www.freetronics.com.au/pages/humid-humidity-temperature-sensor-module-quickstart-guide

Parts required

Here is what you will need:

  • Arduino Uno board
  • Temperature Sensor Module
  • Breadboard
  • 3 male-to-male jumper wires
  • Arduino IDE software (free at http://www.arduino.cc/)
  • USB cable

Wire it up!

tempwiring

 

Attach the temperature sensor module to your breadboard. The GND pin should connect the GND pin on the Arduino board, the VCC pin should connect the 5V pin on the Arduino board, and the DATA pin should connect to the digital I/O pin 2 on the Arduino board – all these are connected via the breadboard using male-to-male jumper wires.

The pictures below show how to wire it all up in greater detail.

tempwiring1 tempwiring2 tempwiring3

 

Arduino code

Here is the code for the Arduino sketch (you can also copy it from http://www.freetronics.com.au/pages/temp-temperature-sensor-module-quickstart-guide). Make sure you scroll down to see how to test the sensor.

#define REF_PIN 2
void getCurrentTemp( int *sign, int *whole, int *fract);
char temp_string[10];
void setup(){
  Serial.begin(9600);
  // initialize DS18B20 datapin
  digitalWrite(REF_PIN, LOW);
  pinMode(REF_PIN, INPUT);      // sets the digital pin as input (logic 1)
  pinMode(15, INPUT);
}
void loop(){
  getCurrentTemp(temp_string);
  Serial.println(temp_string);
  delay(1000);
}
void OneWireReset (int Pin) // reset.  Should improve to act as a presence pulse
{
  digitalWrite (Pin, LOW);
  pinMode (Pin, OUTPUT);        // bring low for 500 us
  delayMicroseconds (500);
  pinMode (Pin, INPUT);
  delayMicroseconds (500);
}
void OneWireOutByte (int Pin, byte d) // output byte d (least sig bit first).
{
  byte n;
 
  for (n=8; n!=0; n--)
  {
    if ((d & 0x01) == 1)  // test least sig bit
    {
      digitalWrite (Pin, LOW);
      pinMode (Pin, OUTPUT);
      delayMicroseconds (5);
      pinMode (Pin, INPUT);
      delayMicroseconds (60);
    }
    else
    {
      digitalWrite (Pin, LOW);
      pinMode (Pin, OUTPUT);
      delayMicroseconds (60);
      pinMode (Pin, INPUT);
    }
    d = d>>1; // now the next bit is in the least sig bit position.
  }
}
byte OneWireInByte (int Pin) // read byte, least sig byte first
{
  byte d, n, b;
 
  for (n=0; n<8; n++)   {     digitalWrite (Pin, LOW);     pinMode (Pin, OUTPUT);     delayMicroseconds (5);     pinMode (Pin, INPUT);     delayMicroseconds (5);     b = digitalRead (Pin);     delayMicroseconds (50);     d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
  }
  return (d);
}
void getCurrentTemp (char *temp)
{
  int HighByte, LowByte, TReading, Tc_100, sign, whole, fract;
  OneWireReset (REF_PIN);
  OneWireOutByte (REF_PIN, 0xcc);
  OneWireOutByte (REF_PIN, 0x44); // perform temperature conversion, strong pullup for one sec
  OneWireReset (REF_PIN);
  OneWireOutByte (REF_PIN, 0xcc);
  OneWireOutByte (REF_PIN, 0xbe);
  LowByte = OneWireInByte (REF_PIN);
  HighByte = OneWireInByte (REF_PIN);
  TReading = (HighByte << 8) + LowByte;
  sign = TReading & 0x8000;  // test most sig bit
  if (sign) // negative
  {
    TReading = (TReading ^ 0xffff) + 1; // 2's comp
  }
  Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25
 
  whole = Tc_100 / 100;  // separate off the whole and fractional portions
  fract = Tc_100 % 100;
 
  if (sign) {
    temp[0] = '-';
  } else {
    temp[0] = '+';
  }
 
  if (whole/100 == 0) {
    temp[1] = ' ';
  } else {
    temp[1] = whole/100+'0';
  }
  temp[2] = (whole-(whole/100)*100)/10 +'0' ;
  temp[3] = whole-(whole/10)*10 +'0';
  temp[4] = '.';
  temp[5] = fract/10 +'0';
  temp[6] = fract-(fract/10)*10 +'0';
  temp[7] = '\0';
}
Testing

Upload the code once you connect the Arduino to the computer with a USB cable. In the Arduino IDE, click on Tools > Serial Monitor. Make sure you change the baud rate to 9600 baud. The temperature sensor should have a constant blue light and the Serial Monitor should display the temperature readings every second.

temptesting temptesting2

Think about how you could use the temperature sensor. You can connect the Arduino to a dot-matrix or LCD display to display the temperature. It could also be used for climate control inside a room. How else could you use it?

Click here if you would like to download a PDF copy of these instructions.

Christmas Lights Arduino project

The project

This project involves green and red LED lights which flash in an alternating pattern. In this project, you will use two LEDs but you can add as many as you want if you have enough jumper wires and LEDs.

Scroll down to view the instructions or click here to download a PDF copy of the instructions.

What you need:

  • 1 x Arduino Uno or compatible board
  • 1 x red LED
  • 1 x green LED
  • 2 x 470 Ohm resistor (yellow-violet-black-black-brown, or yellow-violet-brown-gold)
  • 1 x Solderless breadboard
  • 3 x jumper wires

Photos

Picture1 Picture2 Picture3

Wiring it up

Picture4
The LEDs are connected to pins 12 and 13 and each one has a 470 Ohm resistor connect to GND via the breadboard.

The code

Here is the Arduino sketch code:

/*
Christmas lights - blinking red and green LEDs
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int redLED = 13;
int greenLED = 12;
 
// the setup routine runs once when you press reset:
void setup() { 
 // initialize the digital pin as an output.
 pinMode(redLED, OUTPUT);
 pinMode(greenLED, OUTPUT); 
}
 
// the loop routine runs over and over again forever:
void loop() {
 digitalWrite(redLED, HIGH); // turn the LED on (HIGH is the voltage level)
 digitalWrite(greenLED, LOW); // turn the LED off by making the voltage LOW
 delay(100); // wait for a second
 digitalWrite(redLED, LOW); // turn the LED off by making the voltage LOW
 digitalWrite(greenLED, HIGH); // turn the LED on (HIGH is the voltage level)
 delay(100); // wait for a second
}

Use the Arduino IDE to write this code, verify it and then upload it to the Arduino board. For an extra challenge, why not add a light sensor so that the lights only come on when it’s dark, or a button so that the lights can be switched on and off easily? You can also use a chain of LEDs instead of separately wiring them up to a breadboard.

Making an LED Sensor Light with Arduino

This video tutorial explains how to use an Arduino Uno to make an LED light. A light sensor will detect low-light conditions to turn on an LED and also turn off an LED when light is present.

The project involves an Arduino Uno board, an LED, a light sensor which can detect different levels of light and return a value, and a breadboard with jumper wires.

The way the project works is as follows. Use the light sensor to get light readings constantly. You can specify a value which, when the light is less than this value an LED light will be turned on, and when the light is greater than this value the LED light will be turned off.

This project allows you to work with getting readings from a sensor (input), use if statements to test conditions, and use an LED to produce some form of output.

Click Tools > Serial Monitor and set to 9600 baud to view the light sensor readings received from the Arduino when it is connected via USB to the computer.

Watch the video below or click here to view it on YouTube.

Downloads:

The source code is also available here:

int led = 13;
int lightReading = 0;

void setup () {
  pinMode(led, OUTPUT);
  Serial.begin(9600);
}

void loop () {
  lightReading = analogRead(A2);
  Serial.println(lightReading, DEC);
  if (lightReading < 50) // change this value to suit light conditions. Ranges from 0 (very dark) to 1023 (very bright).
  {
    digitalWrite(led, HIGH); // turns the light on
    Serial.println("light on");
  } 
  else
  {
    digitalWrite(led, LOW); // turns the light off
    Serial.println("light off");
  }
  // Delay of 1000ms before checking light level again
  delay(1000);
  
}

Diagram and photos

ArduinoLDR

The photos below show the same layout although a 470 Ohm resistor is also being used.

1

2 3

Light sensor readings
Light sensor readings

Blinking LED Arduino project

This video tutorial has been designed for those just starting out with Arduino. For this beginner project you will need an Arduino Uno board (or compatible board), an LED of any colour, a 470-Ohm resistor, two jumper wires, a breadboard, a male A-B USB cable, and a computer (Mac/Windows) with the Arduino IDE software installed.

This project involves programming your Arduino Uno to make an LED light blink on and off. To get started, watch the video below or click here to view it on YouTube. Access the links below to get a copy of the code, printed instructions, schematic, and a guide to resistors.

Downloads:

The source code is also available here:

/*
 Blink
 Turns on an LED on for one second, then off for one second, repeatedly.
*/
 
// Give the LED a name - it will be connected on PIN 13
int led = 13;
 
// the setup routine runs once when you press reset:
void setup() { 
 // initialize the digital pin as an output.
 pinMode(led, OUTPUT); 
}
 
// the loop routine runs over and over again forever:
void loop() {
 digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
 delay(1000); // wait for a second (1000 = 1 sec.)
 digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
 delay(1000); // wait for a second
}

Photos

1 2 3 4