SunFounder PiCar-4WD Manual

Smart robot car kit

Advertisement

Preface
About SunFounder
SunFounder is a company focused on STEAM education with products like open source robots and smart
devices distributed globally. In SunFounder, we strive to help elementary and middle school students as
well as hobbyists, through STEAM education, strengthen their hands-on practices and problem-solving
abilities. In this way, we hope to disseminate knowledge and provide skill training in a full-of-joy way, thus
fostering your interest in programming and making, and exposing you to a fascinating world of science
and engineering. To embrace the future of artificial intelligence, it is urgent and meaningful to learn
abundant STEAM knowledge.
About This Kit
PiCar-4WD is a cost-effectively developmental smart car kit of Raspberry Pi, suitable for the Raspberry Pi 3
model B/B+ and 4 Model B. With the grayscale module, ultrasonic module, Photo-interrupter sensor
module, the car can realize obstacle avoidance, line following, object following, cliff detection, velocity
measurement, mileage calculation and so on.
Besides, the kits comes with operational examples of Web terminal, a rookie-friendly design. We also
provide the original python code for
in-depth learning.

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the PiCar-4WD and is the answer not in the manual?

Questions and answers

Summary of Contents for SunFounder PiCar-4WD

  • Page 1 STEAM knowledge. About This Kit PiCar-4WD is a cost-effectively developmental smart car kit of Raspberry Pi, suitable for the Raspberry Pi 3 model B/B+ and 4 Model B. With the grayscale module, ultrasonic module, Photo-interrupter sensor module, the car can realize obstacle avoidance, line following, object following, cliff detection, velocity measurement, mileage calculation and so on.
  • Page 2 Quickly Support Thanks for using the PiCar-4WD Smart Robot Car Kit for Raspberry Pi. You can contact us at the email address below. Please don't hesitate to let us know if you need any support from us. We love helping our customers to learn and play with our products.
  • Page 3 Warning When you purchase or use this product, please note the following points: This product contains many small parts. Swallowing or improper operation may cause serious  infections and death. Seek immediate medical attention once the accident happens. Strictly prohibit use this product and its parts near any AC electrical outlet or other circuits in case of ...
  • Page 4: Table Of Contents

    Contents Component List ..................................1 Structural Plates ................................. 1 Mechanical Parts ................................2 Electronic Parts .................................. 3 Other Parts ..................................4 Driving Parts ..................................6 Self- prepared Part ................................6 Get Started with Raspberry Pi ............................... 7 If You Have A Screen ................................ 8 Required Components .............................
  • Page 5 Initialize the Environment ............................. 31 Module Testing ................................33 Power Supply Detection ............................... 36 Reset Controller ................................36 Hardware Assembling ................................37 Assembling Motor ................................37 Photo-interrupter Module + Encoding Disk ......................38 Assembling Grayscale Sensor Module ........................39 Assembling Battery Holder ............................40 Assembling Servo ................................
  • Page 6: Component List

    Component List Structural Plates...
  • Page 7: Mechanical Parts

    Mechanical Parts...
  • Page 8: Electronic Parts

    Electronic Parts...
  • Page 9: Other Parts

    Other Parts...
  • Page 11: Driving Parts

    Driving Parts Self- prepared Part Note: After opening the package, please check whether the quantity of components is compliance with product description and whether all components are in good condition.
  • Page 12: Get Started With Raspberry Pi

    Get Started with Raspberry Pi Before you complete the assembly, you need to test whether all the modules work properly and keep the servo angle at 90 degrees. In this chapter, configure your Raspberry Pi first. Raspbian system, recommend to install the version :...
  • Page 13: If You Have A Screen

    Depending on the different devices you use, you can start up the Raspberry Pi in different methods. If you have a separate screen for Raspberry Pi, follow the instructions in this chapter. Otherwise, please find the corresponding steps in the following chapters. If You Have A Screen If you have a screen, you can use the NOOBS (New Out Of Box System) to install the Raspbian system.
  • Page 14: Procedures

    Procedures Step 1 NOOBS NOOBS LITE - To download NOOBS from your PC, you can choose the only difference is that there is a built-in offline Raspbian installer in NOOBS, while the NOOBS LITE can only be operated online. Here, you are suggested to use the former. Here is the download address of Noobs: https://www.raspberrypi.org/downloads/noobs/ Step 2 Formatter...
  • Page 15 Step 3 Next, you will need to extract the files from the NOOBS zip archive you downloaded from the Raspberry Pi website.  Find the downloaded archive — by default, it should be in your Downloads folder.  Double-click on it to extract the files, and keep the resulting Explorer/Finder window open. Finally select all the files in the NOOBS folder and copy them to the Micro SD card.
  • Page 16 Step 5 Insert the Micro SD card into the Raspberry Pi. In addition, connect the screen, keyboard and mouse to it. Finally switch on the 4WD-Hat board. Due to the long test time, you can also plug a 2.5A power adapter into the Raspberry Pi.
  • Page 17 Step 6 It will go to the NOOBS interface after starting up. If you use NOOBS LITE, you need to select Wi-Fi networks (w) first. Tick the checkbox of the Raspbian and click Install in the top left corner. The NOOBS will help to conduct the installation automatically.
  • Page 18 Step 7 When the installation is done, the system will restart automatically and the desktop of the system will appear.
  • Page 19 Step 8 If you run Raspberry Pi for the first time, the application of “Welcome to Raspberry Pi” pops up and guides you to perform the initial setup.
  • Page 20 Step 9 Set country/region, language and time zone, and then click “next” again.
  • Page 21 Step 10 Input the new password of Raspberry Pi and click “Next”.
  • Page 22 Step 11 Connect the Raspberry Pi to WIFI and click "Next".
  • Page 23 Step 12 Retrieve update.
  • Page 24 Step 13 Click "Done" to complete the Settings. Now we can run the Raspberry Pi. Note: You can check the complete tutorial of NOOBS on the official website of the Raspberry https://www.raspberrypi.org/help/noobs-setup/...
  • Page 25: If You Have No Screen

    If You Have No Screen If we don't have a screen, we can directly write the Raspbian system to the Micro SD card and we can control the Raspberry Pi on PC remotely by directly modifying the configuration file of the network settings in the Micro SD card.
  • Page 26 Step 3 .img Unzip the package downloaded and you will see the file inside. Note: DO NOT extract the file. Step 4 Plug the USB Card Reader into the computer, then you can burn the image file with the Etcher. Keep the USB card reader plug in your computer.
  • Page 27: Connect The Raspberry Pi To The Internet

    PC use Windows system, then you can't access the directory and what you need next is to go to the directory, to create a new file with the same name, wpa-supplicant. /boot/ conf. Input the following content in the file. ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 country=GB network={ ssid="WiFi-A" psk="Sunfounder" key_mgmt=WPA-PSK priority=1...
  • Page 28: Start Ssh

    You need to replace “WiFi-A” with your custom name of WiFi and “Sunfounder” with your password. By doing these, the Raspbian system will move this file to the target directory automatically to overwrite the keep the USB original WIFI configuration file when it runs next time. After doing this step, you also need to card reader plug in your computer.
  • Page 29: Get The Ip Address

    Switch On/Off Get the IP Address After the Raspberry Pi is powered on with a 2.5A power adapter, we need to get the IP address of it. There are many ways to know the IP address, and two of them are listed as follows. 1.
  • Page 30 If you have permission to log in the router(such as a home network), you can check the addresses assigned to Raspberry Pi on the admin interface of router. The default hostname of the system, Raspbian is raspberrypi, and you need to find it. (If you are using ArchLinuxARM system, please find alarmpi.) 2.
  • Page 31: Use The Ssh Remote Control

    Use the SSH Remote Control We can open the Bash Shell of Raspberry Pi by applying SSH. Bash is the standard default shell of Linux. The Shell itself is a program written in C that is the bridge linking the customers and Unix/Linux. Moreover, it can help to complete most of the work needed.
  • Page 32 Step 4 Input the passcode and the default password is raspberry. Step 5 We now get the Raspberry Pi connected and are ready to go to the next step. Note: When you input the password, the characters do not display on window accordingly, which is normal.
  • Page 33  For Windows Users If you're a Windows user, you can use SSH with the application of some software. Here, we recommend PuTTY. Step 1 Download PuTTY. Open PuTTY and click Session on the left tree-alike structure. Enter the IP address of the RPi in the text box under Host Name (or IP address) and 22 under Port (by default it is 22).
  • Page 34 Step 2 Click Open. Note that when you first log in to the Raspberry Pi with the IP address, there prompts a security reminder. Just click Yes. Step 3 When the PuTTY window prompts “ login as: ” , type in “ pi ” (the user name of the RPi), and password: “raspberry”...
  • Page 35: Download The Code

    ① Change directory to /home/pi. /home/pi/ Note: cd, short for change directory is to change from the current path to the intended directory. Informally, here is to go to the path /home/pi/. ② Clone the repository from github. clone https://github.com/sunfounder/picar-4wd...
  • Page 36: Test The Modules

    Test the Modules In case of anything wrong with the components, you can firstly debug them, then start your assembling and use. Initialize the Environment Initialize the environment before using the product example. The methods are as follows. ① Assemble 4WD-Hat and boot the Raspberry Pi. 4WD-Hat Raspberry Pi...
  • Page 37 Switch On/Off ② Enter the folder picar-4wd. /home/pi/picar-4wd/ ③ Start up the initialization function. sudo python3 setup.py install...
  • Page 38: Module Testing

    A. Download and configure necessary library files, including pip, sysstat, i2c-tools. B. Provide entry points called「picar-4wd」to call the function of example code. Note: Initialization needs connecting to the internet and spending a lot of time. The final line will appear 「finish」after configuration is finished.
  • Page 39 Input the command. picar-4wd test motor After you input the command, the four motors will last working till you press on CTRL+C. Among the motors, two of them rotate clockwise, and the rest of them rotate anticlockwise. ② Testing the grayscale sensor module.
  • Page 40 Input the following command. picar-4wd test grayscale After you input the command, the monitor will print the readings of three detectors of line-following module once. ③ Testing the servo and the ultrasonic module. Start your wire up: Servo Pin(0) Brown ->...
  • Page 41: Power Supply Detection

    Input the commands: picar-4wd test servo The arm of servo will rotate to the angle of 0, meanwhile the monitor will print the readings of ultrasonic module once. Note: You need to boot this function to check the direction of servo’s output axis when you assemble the car and the servo.
  • Page 42: Hardware Assembling

    Hardware Assembling Assembling Motor Assemble 4 motor to the bottom plate. Note: M3*28mm Screw 1) Keep the vertex angle of the triangle sticking upward. M3 Nut 2) For the convenience of later assembly, Support please mark the number(1, 2, 3, 4) on the socket of the 4 motors.
  • Page 43: Photo-Interrupter Module + Encoding Disk

    Photo-interrupter Module + Encoding Disk Insert the Encoding Disk into the motor, then assemble the Photo-interrupter module from bottom to top. Note: Keep the Encoding Disk on the motor away from the Photo-interrupter module to avoid the potential abrasion. M3 Nut Encoding Disk Photo-interrupter Module M3*8 Screw...
  • Page 44: Assembling Grayscale Sensor Module

    Assembling Grayscale Sensor Module Have the Grayscale sensor module be under the head of the car. M3 Nut Grayscale Sensor Module M3*8 Screw...
  • Page 45: Assembling Battery Holder

    Assembling Battery Holder Assemble the battery holder to the top plate and put the battery in. Note: To get the battery out of the holder easily, you can wind the battery with ribbon like this. 18650 Batteries M3*8 Flat-Head Screw Top Plate Battery Case Ribbon...
  • Page 46: Assembling Servo

    Assembling Servo Assemble the servo from bottom to top. Note: the output axis is to face outwards. M2*8 Screw Top Plate Servo M2 Nut...
  • Page 47: Assembling Nylon Standoffs

    Assembling Nylon Standoffs M2.5*8+6mm Nylon Standoff M2.5*6 Screw...
  • Page 48: Assembling Top Plate And Bottom Plate

    Assembling Top Plate and Bottom Plate M3*8 Screw M3*30mm Nylon Standoff M3*8 Screw...
  • Page 49: Assembling Spoiler

    Assembling Spoiler Spoiler...
  • Page 50: Assembling Control Board

    Assembling Control Board M2.5*6 Screw 4WD-Hat M2.5*11mm Nylon Standoff Raspberry Pi...
  • Page 51: Connect Wires

    Connect Wires Connect the modules properly with the guidance of the marks on the 4WD-Hat board. Note:For good appearance, you need to make sure that all the wires go through the bottom plate of the car to the HAT on the upper plate of the car.
  • Page 52: Assembling Radar Support

    Assembling Radar Support Note: Assemble the servo arm only when the Raspberry Pi is energized and the command 「picar-4wd test servo」is running. M1.5*4 Screw Radar Support Servo Screw Servo Arm...
  • Page 53: Assembling Ultrasonic Sensor Module

    Assembling Ultrasonic Sensor Module M1.4 Nut Ultrasonic Module M1.4*8 Screw...
  • Page 54: Assembling Wheels(Finished)

    Assembling Wheels(finished)
  • Page 55: Web Control

    You can use most functions of Picar-4wd, such as obstacle avoidance, line-following, object following and voltage check. Note: Check the next chapter if you want to use python to control Picar-4wd. Boot Web Control Input the command and start up the web control program.
  • Page 56 Interface Introduction...
  • Page 57 1. Arrow Key 2. Power Regulator 3. Obstacle Monitor: checking the obstacle ahead within 35cm and 180°. Red dot represents there is an obstacle ahead detected by ultrasonic module. 4. Grayscale Monitor: displaying the color condition of the detected surface. If the module detects the black lines and then the cliff, the different colors will display on the monitor.
  • Page 58: Setting Page

    drive along the black lines on the ground. If not, please adjust the threshold on the setting page. . Setting: You can enter the Setting page by clicking this one. Setting Page Ultrasonic This page is used to test whether the ultrasonic sensor works well. The slider below can be used to adjust the current direction and angle of the ultrasonic sensor.
  • Page 59 Wheels You can test the working condition of the wheels on this page. Click a wheel then set the POWER, click the two buttons in bottom right corner. When you press the button at left, the wheel goes forward; press the button on the right, the wheel go backward. If the car doesn’t follow your operation like that, you should adjust the direction.
  • Page 60 -a .picar-4wd/ nano config 2) In the configure file, the first 4 lines of codes are used to control the turning of the motor. The last line is used to adjust the angle of the servo. For example, if in the above test, the direction of the motor at...
  • Page 61 3) Rerun the following commands. /home/pi/picar-4wd/ picar-4wd web-example 4) In the browser, click Retry, and you can test the direction of the wheel ready to be used.
  • Page 62 On this page, you can adjust the threshold of the line following and cliff detection. Line Reference Put the Picar-4wd on the white surface and record these three values of Grayscale. Now the value of Grayscale is large enough, about 1500. Then put the grayscale sensor module of Picar-4wd above the black surface and record these three values of Grayscale.
  • Page 64 System You can check the operating condition of the system on this page. Make the example program run automatically at startup. picar-4wd web-example enable If you want to turn off the Start on Boot, please type in the following command.
  • Page 65: Python Program Control

    Python Program Control Python Example Program Python Example Program is in the examples folder. /home/pi/picar-4wd/examples You can run them by using Python3.  keyboard_control.py After running the example, press「W」, 「A」, 「S」, 「D」, and you can get the car going ahead, back, left, right.
  • Page 66  track_line.py If you paste a black lane (width: around 2.5cm) on the white floor, the car will move along the lane. python3 track_line.py  follow.py When the example runs, the car will follow the object ahead. python3 follow.py...
  • Page 67: Python Example Code Analysis

    Python Example Code Analysis Program Structure Framework...
  • Page 68 keyboard_control.py In the code, we use readchar and readkey to get the input state of the key, then we assign some functions to the keys by using if statement. import picar_4wd import import import termios import asyncio power_val 'status' readchar(): ……...
  • Page 69 …… elif key=='s': …… elif key=='d': …… else: …… key=='q': print("quit") break __name__ == '__main__': Keyborad_control() obstacle_avoidance.py In the code, the distance measurement radar (ultrasonic + servo) will take sample for 11 times in total (once every 18°). Then it makes judgement on the distance states of the fourth and the seventh sampling. If radar indicates no obstacle(the returned value is 2), the car will continue moving ahead, or else the car turn right to get around the obstacle.
  • Page 70 = scan_list[3:7] print(tmp) != [2,2,2,2]: fc.turn_right(speed) else: fc.forward(speed) __name__ == "__main__": try: main() finally: fc.stop() track_line.py In this code, grayscale sensor module will judge the gray level of the floor under the car’s head. The car adjusts the directions according to the detection values of these three detectors to follow the black line. Note: About Grayscale judgement, refer to 「get_line_status()」in 「_init_.py」for more details.
  • Page 71 fc.turn_right(Track_line_speed) __name__=='__main__': while True: Track_line() follow.py In the code, the ultrasonic module will take sample to form an array containing 11 elements; the module detects once every 18 ° and puts the distance state (0 , 1 , 2) of every angle into the array. If there is something obstructive at the right front, the detection value is [22222211122].
  • Page 72 continue scan_list = [str(i) scan_list] scan_list = "".join(scan_list) paths = scan_list.split("2") length_list = [] path paths: length_list.append(len(path)) max(length_list) == 0: fc.stop() else: = length_list.index(max(length_list)) = scan_list.index(paths[i]) += (len(paths[i]) - 1) / delta = len(scan_list) / < delta: fc.turn_left(speed) elif > * delta: fc.turn_right(speed) else:...

Table of Contents