How to setup Arduino with Motors and sensors

Posted by Mohammed 27/10/2016 0 Comment(s) Microcontrollers,Electronic components and parts,

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

We are also an authorized Arduino distributor for the UAE, Oman, Qatar, Saudi, Kuwait and Bahrain. We can provide you with any product of your choice. Please, contact us for inquires.

 

Our Arduino training series:

1. Start with Arduino - Zeroohm Arduino Training

2. Start with Arduino - Arduino with Motors and sensors

3. Start with Arduino - Wireless communication

4. How to setup Arduino with room temperature monitoring using LM36 sensor

5. Introduction to IoT using Arduino

5. Introduction to IoT using Arduino

6. IoT: Humidity and Temperature Sensor

7. ADXL345 Module Communication with Arduino UNO Board

 

 

 

 

Arduino Zeroohm workshops aim to let beginners get started with Arduino using Sparkfun Inventor Kit:

-    What is a sensor and their different types.

-    What is a motor and their different types.
-    How to read datasheets.
-    How to control motors and sensors by writing Arduino codes.

  Experiment 1: Photo Resistor
Photo Resistors is a light-dependent resistor (LDR); which converts light energy into electrical energy. The resistance of a photo resistor decreases with increasing incident light intensity.
In this task, you're going to switch an LED on and off, depending on the room's light by the following steps.
1.    Connect the following Circuit using the shown components in Fig.1.

 

2.    Open Arduino IDE software and start writing the following code:
int sensorPin = 0;                    //define a pin for Photo resistor
int ledPin=9;                         //define a pin for LED
void setup()
{
    Serial.begin(9600);                  //Begin serial communication
    pinMode( ledPin, OUTPUT );
}

void loop()
{
    int lightLevel;        
    lightLevel=(analogRead(sensorPin)/2);    
    Serial.println(lightLevel);
    if (lightLevel<200)
    digitalWrite(ledPin, HIGH);
    else
    analogWrite(ledPin, LOW);
    delay(10);
}

3.    Check that your code is correct and upload it to the RedBoard.
4.    Vary the room light and notice what happens to the LED.

    Discussion:

-    In order to display the value that we’re getting from the sensor, we’ll have to use the serial monitor in the Arduino IDE. The serial monitor is a separate pop-up window that acts as a separate terminal that communicates by receiving and sending Serial Data.

-    To start the serial communication, we use Serial.begin(). The serial.begin() sets the data rate in bits per second (baud) for serial data transmission.

-    Serial.Println() prints data to the serial port as human-readable.

 

    Experiment 2: Temperature Sensor
Temperature Sensor is a thermocouple that provides for temperature measurement through an electrical signal. A change in temperature of one degree centigrade is equal to a change of 10 mV at the sensor output. The temperature sensor we're using (TMP36) has a nominal 750 mV at 25 degree C.
In this task you’ll switch LED ON and OFF depending on the room temperature by following these steps:
1.    Connect the following Circuit using the shown components in Fig.2.

 

 

2.    Open Arduino IDE software and start writing the following code:

const int sensorPin = 0;
int ledPin = 9;
void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop()
{  
  float voltage, degreesC, degreesF;
  voltage = getVoltage(sensorPin);
  degreesC = (voltage - 0.5) * 100.0;             // from datasheet
 
  Serial.print("voltage: ");
  Serial.print(voltage);
  Serial.print("  deg C: ");
  Serial.print(degreesC);
  Serial.println(degreesF);
  delay(1000);   

  if (degreesC >20)
    digitalWrite(ledPin,HIGH);
  else
    digitalWrite(ledPin,LOW);
}

float getVoltage(int pin)
{
  return (analogRead(pin) * 0.004882814);         // 0.004882814= (5/1024)
}

3.    Check that your code is correct and upload it to the RedBoard.
4.    Vary the temperature and notice what happens to the LED.

 Discussion:

-    In this task we’ve defined another function called getVoltage, this function saves the value of the sensorpin and display it on the serial monitor. Functions help the programmer stay organized, and codify one action in one place so that the function only has to be thought out and debugged once.

-    Functions can either be written before the loop function of after it.

-    In this code we’re converting the voltage we’re getting from the sensor to a temperature value using a mathematical formula that differs from one sensor to another. This formula can be found in the datasheet of the sensor.

-    By using the if statement, we turn the LED on when temperature is higher than 20C and turn it off if it’s less than 20C.

 


   Experiment 3: DC Motor
A motor is a mechanical or electrical device that creates motion. An electrical motor is an electrical machine that converts electrical energy into mechanical energy.
In this task you will control the speed of a DC motor using the RedBoard by following these steps:

1.    Connect the following Circuit using the shown components in Fig.3.

 

 

2.    Open Arduino IDE software and start writing the following code:
int motorPin = 9;
 
void setup()
{
  pinMode(motorPin, OUTPUT);
  Serial.begin(9600);
  while (! Serial);
  Serial.println("Type a speed (0-255) into the box above,");
  Serial.println("then click [send] or press [return]");
  Serial.println();                          // Print a blank line

 }
 


void loop()
{
  if (Serial.available())
  {
    int speed = Serial.parseInt();
    if (speed >= 0 && speed <= 255)
    {
      Serial.println(speed);
      analogWrite(motorPin, speed);
      delay(100);
    }    
    else
    {
      analogWrite(motorPin, 0);
      delay(100);
    }    
  }
}

3.    Check that your code is correct and upload it to the RedBoard.
4.    Vary the input speed and notice the movement of the motor.

   Discussion:

-    In this code we’ve set a displaying message that asks the user to input a speed value, such that the motor will move according to that value.

-    We print the entered value using the function serial.Println(), and we move the motor in that entered speed by using the analogWrite() function.

-    In case users input a negative or a non number value the motor will not respond, this is use of the Serial.parseInt() function; where the Initial characters that are not digits or a minus sign, are skipped.
 

 

  Experiment 4: Servo Motor
A servomotor is a rotary actuator or linear actuator that allows for precise control of angular or linear position, velocity and acceleration.  It consists of a suitable motor coupled to a sensor for position feedback.
In this task you will vary the position of the motor by following these steps:

1.    Connect the following Circuit using the shown components in Fig.4.

2.    Open Arduino IDE software and start writing the following code:
#include <Servo.h>          // servo library
Servo servo1;              // servo control object
void setup()
{   
servo1.attach(9);
}
void loop() {
 servo1.write(90);                // Tell servo to go to 90 degrees
delay(1000);
servo1.write(180);                 // Tell servo to go to 180 degrees
delay(1000);                         // Pause to get it time to move
servo1.write(0);                   // Tell servo to go to 0 degrees
delay(1000);               // Pause to get it time to move  
  }       
        
3.    Check that your code is correct and upload it to the RedBoard.
4.    Notice the Servo Motor movements.
   Experiment 5: Relay
 Relays are switches that open and close circuits electromechanically or electronically.  They control one electrical circuit by opening and closing contacts in another circuit.
In this task you will close and open a relay to switch ON and OFF a LED  by following these steps:

1.    Connect the following Circuit using the shown components in Fig.5.

2.    Open Arduino IDE software and start writing the following code:
const int relayPin = 2;                          // use this pin to drive the transistor
void setup()  {
 pinMode(relayPin, OUTPUT);          // set pin as an output
}
void loop()  {
  digitalWrite(relayPin, HIGH);          // turn the relay on
  delay(1000);                          // wait for one second
  digitalWrite(relayPin, LOW);            // turn the relay off
  delay(1000);                          // wait for one second
}
        
3.    Check that your code is correct and upload it to the RedBoard.
4.    Notice LED when relay is switched ON and OFF.
    Experiment 6: Fan
In this task we’ve combined experiment 2 and 3 in order to build a small fan using the DC motor and the temperature sensor by following these steps:

1.    Connect the following Circuit using the shown components in Fig.6.

 

2.    Open Arduino IDE software and start writing the following code:
const int sensorPin = 0;
int motorPin = 9;
int ledPin = 8;

void setup()
{
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}
void loop()
{  
  float voltage, degreesC, degreesF;
  voltage = getVoltage(sensorPin);
  degreesC = (voltage - 0.5) * 100.0;             // from sensor datasheet


  Serial.print("voltage: ");
  Serial.print(voltage);
  Serial.print("  deg C: ");
  Serial.print(degreesC);
  Serial.println();   
  delay(1000);
 
  if (degreesC >20)
  {
    analogWrite(motorPin, 100);              // turns the motor On
    delay(3000);
  }  
  else
  {
    analogWrite(motorPin, 0);              // turns the motor Off
    delay(3000);
  }
}

float getVoltage(int pin)
{
  return (analogRead(pin) * 0.004882814);     // 0.004882814= (5/1024)
}

3.    Check that your code is correct and upload it to the RedBoard.
4.    Notice what happens to the motor when you increase the temperature.

   Discussion:

-    This code is nothing but a combination of experiment 2 and 3 codes, the only difference is that the movement of the motor depends on the temperature value of the sensor; where the motor moves at speed 100 when temperature is greater than 20C and stops moving when it’s less than 20C.  

 

 


Note: Zeroohm provides many different Arduino, Raspberry Pi, Soldering, tailor made trainings. Please contact us for any inquires.

If you like it, please share it! We hope to have as many people learn as possible.

 

Leave a Comment