How to control a DC motor via Arduino Uno as a slave and Mega 2560 as master? Hi I want to control a DC motor using Arduino mega2560 and Arduino Uno as a slave. Mega have to process data from an ultrasonic sensor(measuring distance) and control the motor based on the input. I tried to google but couldn't find a better tutorial or code. I'm using Adafruit motor shield l293d. I have the idea about the wiring part just need a simple code.

Eg:   

 if( distance &lt; 20) {
   //send signal to slave to run the motor forward
 }else {
   //send signal to slave to stop the motor 
 }
Serial is quite easy to implement and use the SoftSerial.

Example code for serial can be found in the Arduino IDE under the menu:

File | Examples | 04. Communication