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.


 if( distance < 20) {
   //send signal to slave to run the motor forward
 }else {
   //send signal to slave to stop the motor 
  • You might look at i2c communication examples, and motor shield examples. Note that the older L293 based shields are very lossy compared to newer v2 shields with the TB6612FNG FET driver. There's also no apparent reason in your question to use two distinct Arduinos for this - it would be farsimpler to use the Uno for everything. – Chris Stratton Sep 26 '17 at 17:11
  • I2C, SPI, UART, ... There are tons of protocols. UART is the simplest, but you won't be able to use the USB port (and so even program it) of the UNO without detaching the MEGA. The MEGA, OTOH, has several HW UART. The I2C is a bit more complicated, but not so much. You will be able to further expand the network in the future. The SPI is the fastest, but requires more wires (4, rather than 1 or 2). The UART will enable you to send back data from the UNO to the MEGA too, while I2C and SPI will require the MEGA to ask the UNO for information if you need to transfer back some data... – frarugi87 Sep 27 '17 at 12:24

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
  • Serial is arguably harder to implement between programs, because you don't have the natural message delineation of something like I2C or SPI. Software serial also complicates other processor operations. – Chris Stratton Sep 27 '17 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.