This project incorporates two Arduino boards, in a Master/Slave setup. The master is one with an accelerometer (ADLX345) and the slave has a Servo attached (SG90). When tilting the accelerometer, the master Arduino tells the Slave Arduino to rotate the servo. This is a great way to learn slave and master serial communication.
Parts you'll need:
- ADLX345 Accelerometer
- SG90 Mini Servo
- Mini Breadboard
- Jumper Leads
- General-Purpose Hookup Wire
- 2x Arduino Uno R3
- Straw (Grab this from your local store)
Step 1 - Wiring:
Wire the ADLX345 to Master Arduino, the Servo to the slave Arduino, using the wiring diagrams below. Note that some servos are brown, red and orange wiring. (Negative, Positive, Signal). Then connect the Arduino's to each other and make any other connections as below.
Step 2 - Make it:
Attach Slave Arduino & Servo to a board, then make you 'finger' by threading some string through a straw, and taping it at the end. Next, make a half cut in the straw so that when the string is pulled, the top end of the straw raises. Hot Glue this to the board to hold it steady. Excuse the hot glue wisps and tails! I'm going for a low-pro setup.
Step 3 - Code it:
The code that I used for this project was written by Nicholas Zambetti and appeared in the examples for the Wire library - Upload Master.ino to the master Arduino.
// Wire Slave Receiver // by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library // Receives data as an I2C/TWI slave device // Refer to the "Wire Master Writer" example for use with this // Created 29 March 2006 // This example code is in the public domain. #include <Wire.h> #include <Servo.h> Servo myservo; void setup() { Wire.begin(8); // join i2c bus with address #8 Wire.onReceive(receiveEvent); // register event Serial.begin(9600); // start serial for output myservo.attach(9); } void loop() { delay(100); } // function that executes whenever data is received from master // this function is registered as an event, see setup() void receiveEvent(int howMany) { while (1 < Wire.available()) { // loop through all but the last char c = Wire.read(); // receive byte as a character Serial.print(c); // print the character } int x = Wire.read(); // receive byte as an integer Serial.println(x); // print the integer myservo.write(x); }
Upload Slave.ino to the slave Arduino;
// Wire Master Writer // by Nicholas Zambetti <http://www.zambetti.com> // Demonstrates use of the Wire library // Writes data to an I2C/TWI slave device // Refer to the "Wire Slave Receiver" example for use with this // Created 29 March 2006 // This example code is in the public domain. #include <Wire.h> void setup() { Wire.begin(); // join i2c bus (address optional for master) } byte x = 0; void loop() { serial(x); delay(1000); x = x + 20; } void serial(byte x) { Wire.beginTransmission(8); // transmit to device #8 Wire.write("x is "); // sends five bytes Wire.write(x); // sends one byte Wire.endTransmission(); // stop transmitting }
The code works by the Master Arduino processing the accelerometer info, and sending a "rotation amount"' to the slave, which would servo.write(rotation amount). You can see the effect this has down below!