Self Driving RC Car

A scaled down version of self-driving system using Neual Networks and OpenCV. The system comprises of –

  • Raspberry Pi with a camera and an ultrasonic sensor as inputs,
  • Server that handles:
    • Steering using NN predictions
    • Stop sign and traffic light detection using Haar feature based Cascade Classifiers
    • Distance measurement through monocular vision
    • Front collision avoidance using ultrasonic sensor
  • RC Car, and,
  • an Arduino board for RC car control

See the self-driving car in action (Note: an improved video is on its way!)

Train data collection process


Train data set


Obstacle detection during autonomous driving



  • Server
    • OpenCV ver3.2+
    • Pygame
    • NumPy
    • PiSerial
  • RaspberryPi
    • PiCamera
  • Arduino
  • RC Car

Project structure

  • arduino/rc_driver/rc_driver.ino: An arduino sketch that acts as a middleware between the RC controller and the server. It allows the user to send commands to drive the car via USB serial interface
  • rpi/
    • transmits the distance measuerment taken by the ultrasonic sensor over a TCP/IP socket to the server
    • streams the captures JPEG video frames over a TCP/IP socket to the server
    • contains Utility function
  • server/
    • cascade_classifiers/
      • contains the trained Haar-feature based Cascade Classifier xml files
    • collected_images/
      • Images captured during the data set collection phase
    • data_set/
      • Data set for training and testing the Neural Network. Stored in .npz format
    • mlp_xml/
      • Trained MLP_ANN paramters in an XML file
    • a multi-threaded server program that captures the video frames and distance measurements streamed from the RPi, steers the car using the predictions from the NN, and provides stop sign and traffic light detection and front collision avoidance capabilities.
    • Receives the streamed video frames and labels them based on the user input for NN training
    • Neural network training using OpenCV
  • test/
    • script to test streaming of distance data from RPi to server
    • script to test streaming of video frames from RPi to server
    • tests RC car control with keyboard
  • utils/
    • contains utility functions


  • Flash Arduino: Flash the Arduino with the rc_driver.ino sketch. For testing purposes, run to drive the RC car with the keyboard

  • Collect data set (for NN training and testing purposes): First run on the server and then run on the RPi. Use the arrow keys on the keybaord to drive the car around a track. The frames are saved only when there is a key press action. When finished driving, press “q” to exit. The data will saved in a npz file under data_set folder.

  • Neural network training: Next, run The NN training duration can vary depending upon the model hyperparameters chosen. Once the training is complete, the network accuracy on the training and test set will be displayed. Following this, the network weights/parameters will be saved in a xml file under mlp_xml.

  • Pi Camera calibration: Take multiple chess board images using the RPi camera at various angles and put them into the chess_board folder. Then, run It will return the camera matrix which should be entered into This matrix will be used for distance measurement by the car while its self driving.

  • Self-driving in action: First run to start the server and then run “” and “” on raspberry pi.


View Github