More electronic components

You are already one step closer to building your own robot as you already know the basics of electronics and have developed few applications using Raspberry Pi. To take further steps in this direction, in this chapter, you will learn how to develop a remote-controlled robot that will be able to live stream video over the Internet and also send distance of the nearest object in real time. Before you move ahead, you need to understand a few more electronics components:

  • Introduction to motors
  • Introduction to multimeter
  • The robotic base
  • The level converter
  • Motor driver IC / H-Bridge
  • The Raspberry Pi camera
  • An ultrasonic sensor
  • The Raspberry Pi battery
  • The Raspberry Pi WiFi module

Introduction to motors

As mentioned earlier, in this chapter, you will develop a remote-controlled robot with some add-on features, so the first thing that you need to make a robot movable are wheels. Now, once you got the wheels, you need some mechanism that can control the speed of the wheels. Motors are used for this purpose; they convert electrical energy into mechanical energy. A world without electric motors is difficult to imagine. From the tiniest motor found in a quartz watch to a million-plus horsepower motor powering a ship, motors are used in many diverse applications. The following screenshot is an example of a motor:

Introduction to motors

There are basically two types of conventional electrical motor available: AC type motors and DC type motors. AC motors are generally used in high power single or multi-phase industrial applications, where a constant rotational torque and speed is required to control large loads such as fans or pumps. DC motors are used in many electronics, positional control, microprocessor, PIC, and robotic circuits. Another DC motors that is commonly used in robotics is the stepper motor. This is particularly well suited to applications that require accurate positioning and a fast response to starting, stopping, reversing, and speed control. Another key feature of the stepper motor is its ability to hold the load steady once the required position is achieved. For example, in a normal DC motor, once you stopped the power to motor, it will still go to some distance because of the continuous motion, while in a stepper motor, once you stop the power, it will stop instantaneously.

Note

There are many other DC motors as well, but discussion of all motors is out of the scope for this book. In this book, you will be using a normal DC motor.

Introduction to multimeter

A multimeter is an electronic measuring instrument that combines several measurement functions in one unit. A typical multimeter can include features such as the ability to measure voltage, current, and resistance. It is also known as a volt-ohm meter (VOM). It can be of two types: analog multimeter and digital multimeter based on the circuit used in the construction. An analog multimeter usually shows results using a pointer that moves over a scale calibrated for all the different measurements that can be made, while on the digital multimeter digits are displayed on the screen. A typical digital multimeter is shown in the following image:

Introduction to multimeter

A typical analog multimeter is shown in the following image:

Introduction to multimeter

Most of the time, you will be using digital multimeter, so you need to understand the capabilities and usage of digital multimeter. On the bottom-right corner of the multimeter, you will see three holes where you can connect a measuring cable. If your circuit operates on higher currents, connect the anode (typically a red probe) to the first hole and connect the cathode (typically, a black probe) to the last hole. The second hole is for circuits operating at low current. Most of the time, you will be using the second hole, where you will connect the anode (typically, a red probe) and the third hole, where you will connect the cathode (typically, a black probe). Here are some features that you will be using while developing a robot in this chapter:

  • Section A: This is for measuring DC voltage. It can measure voltage in a different range. For example, the multimeter shown in the preceding figure can measure voltage up to 1000V.
  • Section B: This is for measuring resistance. While we can calculate the resistance of the resistor using the color code described in Chapter 3, Introduction to Electronics, using multimeter you can quickly measure the resistance. It can measure resistance in different ranges. For example, the multimeter shown in the preceding picture can measure resistance up to 2000K Ohm.
  • Section C and Section D: These features are used for measuring AC voltage and AC current respectively. You will rarely use these functionalities as electronics components work on DC voltage and current.
  • Section E: As a beginner, you will be using this section more often. This section is to check the continuity in the circuit. In any electronics circuit that you will develop, the first thing that you should do if your circuit doesn't work is to check the continuity in the circuit. Connect anode and cathode of the multimeter to the circuit across which you want to test the continuity. This will beep when circuit conducts.
  • Other: Additionally, some multimeters can also measure temperature in degrees Celsius or Fahrenheit, with an appropriate temperature test probe, often a thermocouple.

Robotic base

Once you have the motors for the movement, you need some kind of base that can hold all the motors at a certain distance for the proper movement of the robot. Apart from holding the motor, you need some firm base on which you can place all the electronics components. In this chapter, you will build a 4-wheel robot, so you will require a 4-wheel robotic base. You will be able to get it from a nearby electronics shop. Have a look at the following image to get a clear idea of how it will look in the real world:

Robotic base

Level converter

Because most of the electronics power supply provide 5V and most modern sensors displays are 3.3V only, you need to perform level shifting/conversion to protect the 3.3V device from 5V. Although you can use resistors to make a simple divider, for high-speed transfers, the resistors cause a havoc that is tough to debug. For this reason, you have to use a level converter. Those converters can be of two types: unidirectional and bi-directional. As the name suggests, a unidirectional converter can only convert in one direction, that is, either from 5V to 3.3V or 3.3V to 5V. While on the other end, a bi-directional converter can convert 5V to 3.3V as well as 3.3V to 5V.

Note

Later in this book, you will use a resistor divider circuit to step down the 5V to 3.3V.

Motor driver IC

Depending on the DC motor used, it requires voltage varying from 5V to 36V. Raspberry Pi GPIO can't provide such high range of voltage, so you need some kind of level converter. Moreover, you need to drive the motor in both directions, so some special circuitry is required for this purpose. That special circuit is embedded in a typically used motor driver L293D IC. It works on the concept of H-bridge. Before you understand the functionality of L293D, you need to understand what is H-bridge and the basic functionality of the H-bridge circuit.

H-Bridge

H-Bridge is an electronic circuit that enables the voltage to be applied across a motor (in general, any load) in both directions. It can be built using four switches. The following diagram shows the basic structure of an H-bridge:

H-Bridge

Generally, it is used to change the direction of the motor, but it can also be used for other two purposes:

  • "Brake" the motor: When the motor terminals are shortened, it will come to a sudden stop
  • "Free run" the motor: When the motor terminals are disconnected from a circuit, it will free run to a point

S1

S2

S3

S4

Result

Close

Open

Open

Close

The motor moves right

Open

Close

Close

Open

The motor moves left

Open

Open

Open

Open

The motor free runs

Open

Close

Open

Close

The motor brakes

Close

Open

Close

Open

The motor brakes

Close

Close

Open

Open

Shoot-through

Open

Open

Close

Close

Shoot-through

Close

Close

Close

Close

Shoot-through

In the preceding table, you can see there is one more condition "Shoot-through". When the power supply gets directly shorted, it is called "Shoot-through". You should never use these conditions in your circuits.

Motor driver IC L293D has two H-bridge circuits, so you can control a set of two DC motors simultaneously in any direction. This is a 16-pin dual H-bridge motor driver integrated circuit. Due to its small size, it is widely used in robotics applications to control the DC motors. The pin diagram of the L293D IC is shown in the following diagram:

H-Bridge
  • Enable Pins: Pin 1 and Pin 9 enable pins. Both pins need to be HIGH if you want to use both H-bridge of the IC. For driving the motor with left H-bridge, you need to enable Pin 1 to HIGH. Similarly, to drive the motor with right H-bridge, you need to enable Pin 9 to HIGH. If one of the Pin 1 or Pin 9 goes low, then the motor in the corresponding section will suspend working.
  • Power Supply: Pin 16 is an internal power supply pin where you need to connect 5V power supply. Pin 8 is for motor supply. Depending on the motor used, you can connect corresponding power supply required for that motor to Pin 8. ADD MORE DETAILS
  • Output Pins: As mentioned before, L293D can drive two DC motors simultaneously. One motor should be connected to Pin 3 and Pin 6. Similarly, another motor can be connected to Pin 11 and Pin 14.
  • Ground Pins: Pin 4, Pin 5, Pin 12, and Pin 13 are ground pins. These pins should be connected to GROUND/LOW.
  • Input Pins: There are four input pins on the IC. Pin 2 and Pin 7 are for regulating the motor connected to the left side of the IC, and similarly, Pin 10 and Pin 15 are for regulating the motor connected to the right side of the IC. The motors are rotated based on the input provided across these inputs.

The following table shows the inputs provided and the corresponding result:

Pin 2 / Pin 15

Pin 7 / Pin 10

Result

HIGH

LOW

The motor moves in clockwise direction

LOW

HIGH

The motor moves in anticlockwise direction

LOW

LOW

No rotation

HIGH

HIGH

No rotation

The Raspberry Pi camera

Since the launch of Raspberry Pi board A, people all around the world are exploring it in numerous ways. Computer scientists and researchers have explored the USB camera with Raspberry Pi. However, not all the USB cameras are supported with Raspberry Pi, so Raspberry Pi foundation decided to build a camera module that can directly connect with the Raspberry Pi CSI connector. The 5MP camera module is capable of capturing 1080p video and still images. The module is around 25mm*20mm*9mm and weighs just over 3 gm, which makes it perfect for mobile or other applications where size and weight play an important role.

Setting up the camera

As mentioned earlier, before powering up the Raspberry Pi, connect the Raspberry Pi camera module to the board using CSI connector. The following steps will help you set up the camera:

  1. Now, you have connected the hardware module to the Raspberry Pi. Before you can start taking photos using camera module, there are some software installation that needs to be done with the following commands:
    sudo apt-get update
    sudo apt-get install python-picamera
    

    python-picamera is a pure Python interface to the Raspberry Pi camera module for Python 2.7 and above. The python3 package can be installed using the following command:

    sudo apt-get update
    sudo apt-get install python3-picamera
    
  2. Before you start using the camera module, you need to enable the camera in the Raspberry Pi configuration file.

    Open the raspi-config tool from the terminal:

    sudo raspi-config
    

    Select the Enable camera and hit Enter, then go to Finish. Reboot the Raspberry Pi. Now, you can go ahead and start using your camera module.

Usage of modules

In this section, you will learn how to use the module by taking a picture and capturing a video using the Raspberry Pi camera module.

Taking a picture

Create a file cameraImage.py with the following script:

import picamera
camera = picamera.PiCamera()
camera.capture('image.jpg')

The first line makes the picamera library available to the script. The second line will create the instance of the PiCamera class. The third line will take the picture and store it in the image.jpg file.

You have used the Python script to capture an image using the camera module. You can also use the command-line tool that is available to capture an image. raspistill is the command-line tool for capturing still images with the camera module. Once you have connected and enabled the camera, run the following command in a terminal to take a picture:

raspistill -o cam.jpg

The raspistill command will take a picture from the camera module, the -o flag will store data to the output file and the cam.jpg filename, where you want to save the data.

Note

There are many other options using which you can control the properties of the image taken from a camera module. Some of these options will be discussed in Chapter 6, Image Processing Algorithms.

Recording a video

Similar to the previous section, you can use either the command-line tool or can use the Python script to take a video from the camera module. First off, you will take a video using the Python script.

Create a file cameraVideo.py with the following script:

import picamera
from time import sleep
camera = picamera.PiCamera()
camera.start_recording('video.h264')
sleep(5)
camera.stop_recording()

The first two lines make the picamera and time.sleep libraries available to the script. Line 3 will create the instance of the PiCamera class. The fourth line will start recording the video and store it as video.h264. The fifth line will wait for 5 seconds using the sleep command. The sixth line will stop the recording and the video clip will get saved as video.h264.

The raspivid is the command-line tool for capturing videos with the camera module. Once you have connected the camera, run the following command in terminal to take a video:

raspivid -o video.h264 -t 10000

This will record a video for 10 seconds.

The raspivid command will capture a video from the camera module, the -o flag will store the data to the output file and the video.h264 filename where you want to save the data.

If you do not give any -t flag with the length of the video, by default, raspivid will capture a video for 5 seconds.

An Ultrasonic sensor

Ultrasonic sensors work on a principle similar to radar or sonar, which calculates properties of a target by interpreting the echoes from radio or sound waves respectively. This sensor will generate the high frequency sound waves and evaluate the echo, which is received back by the sensor by measuring the time interval between sending the signal and receiving the echo to calculate the distance to an object. In this chapter, you will be using an HC-SR04 ultrasonic sensor Arduino module. It uses sonar to determine the distance to an object. It gives distance with the nearest object with high accuracy and stable reading in an easy-to-use package for distance ranging from 2 cm to 400 cm. Unlike a sharp sensor, which can also be used for measuring the distance, its operation is not affected by sunlight. The following image is an example of ultrasonic sensors:

An Ultrasonic sensor

As you can see in the preceding image, there are four pins on HC-SR04:

Vcc: +5VDC

Trig: Trigger input for sensor

Echo: Echo output of the sensor

GND: Ground

First, you need to send the trigger pulse of 10 µs (micro Second) to the Trig Pin, which will enable the ultrasonic sensor. The ultrasonic sensor module will send the output to the Echo pin.

Note

The module is not suggested to connect directly to electricity, if connected to electricity, the GND terminal should be connected to the module first; otherwise, it will affect the normal work of the module.

The Raspberry Pi battery

There are many applications where you might want to use Raspberry Pi as a standalone device. The Internet connection and power supply are the only two things that will hinder the use of Raspberry Pi as a standalone device. The Internet connectivity issue can be resolved using the Raspberry Pi WiFi module. To resolve the power supply issue, a rechargeable battery can be used.

The Raspberry Pi Wi-Fi module

As mentioned in the previous section, to make your Raspberry Pi a standalone device, you need to get rid of the Ethernet cable. For this reason, you will be using the Raspberry Pi WiFi module to use the WiFi network. As you know Raspberry Pi doesn't have the capability to connect to the WiFi network, you will be using $5 WiFi module. To make it work, you need to update the current Internet configuration. Here are a few steps that you need to follow to make the WiFi module work properly:

  1. Open the terminal and write the following line of code:
    sudo nano /etc/network/interfaces
    
  2. You will see something like this in the nano text editor:
    auto lo
    iface lo inet loopback
    iface eth0 inet dhcp

    This is a very basic configuration that is used to connect Raspberry Pi to the Internet using an Ethernet cable. You need to modify the interface file to enable the Wi-Fi module. Add the following lines to the interface file:

    allow-hotplug wlan0
    iface wlan0 inet dhcp
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
    iface default inet dhcp

    Once you have edited the file, press CTRL + X to save the file and exit nano editor.

  3. Open the terminal and write this:
    sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
    

    Delete all the content of the file and add the following lines to the file:

    ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
    update_config=1
    network={
    ssid="YOURSSID" #It is same as your WiFi network name
    psk="YOURPASSWORD" # This is your secret network password
    
    # Protocol type can be: RSN (for WP2) and WPA (for WPA1)
    proto=WPA
    
    # Key management type can be: WPA-PSK or WPA-EAP (Pre- Shared or Enterprise)
    key_mgmt=WPA-PSK
    
    # Pairwise can be CCMP or TKIP (for WPA2 or WPA1)
    pairwise=TKIP
    
    #Authorization option should be OPEN for both WPA1/WPA2 (in less commonly used are SHARED and LEAP)
    auth_alg=OPEN
    }

    Once you have edited the file , press CTRL + X to save and close the file.

  4. Now, plug in your WiFi module to the USB port of Raspberry Pi. Updation in details such as network configuration are required; you need to reboot to apply those changes. Once again, open the terminal and restart Raspberry Pi using the following command:
    sudo reboot
    
  5. When the device finishes rebooting, it should automatically connect to the WiFi network. Open Midori browser and try accessing the Internet to check whether Raspberry Pi is connected to Internet or not.

In this section, you were introduced to a few more electronics components that will be used in the next section for building the remote-controlled robot.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.157.62