The next phase of my research into robots is getting motors to function for movement and other things like a crane for lifting. I took the design from Jeremy Blum’s book Exploring Arduino page 76.   I didn’t have his exact components.  So I made a few substitutions.   The project uses a 9V battery and only the ground is tied to the project.   I split the project so the top half of the board is the ground side.  Bottom half of board is the power side.  I tried to logic out my projects in the ground and power sides of the project.

  • NPN 2222A transistor
  • 1 kΩ resistor
  • 1N4007 diode
  • 22μf capacitor

The emitter side of the 2222A goes to ground. 

The base pin has the 1 kΩ resistor going to pin 9 on the Arduino Mega 2560.  

The collector pin goes to the negative side of the capacitor and diode with the blue wires. 

The motor has one wire to the negative side of the diode and the other to the positive side of the diode along with the 9V power

I smoked 3 x 2222A transistors building them project.   You need to pay attention to the direction of the 2222A and diode.  The diode has a silver ring on one side for is the cathode (+) end.   Make sure you don’t have power coming from the Arduino to this breadboard.   Make sure you have the common ground on this breadboard. 

I will be modifying the projects and moving the code of ino files. 

// Include the required Wire library for I2C
// Include the libraries:
// LiquidCrystal_I2C.h: https://github.com/johnrickman/LiquidCrystal_I2C
#include // Library for I2C communication
#include // Library for LCD

// Wiring: SDA pin is connected to A4 and SCL pin to A5.
// Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd16x2_23 = LiquidCrystal_I2C(0x23, 16, 2); // Change to (0x23,16,2) for 16x2 LCD.
LiquidCrystal_I2C lcd20x4_27 = LiquidCrystal_I2C(0x27, 20, 4); // Change to (0x27,20,4) for 20x4 LCD.

#include
int x = 0;
int LED = 13;
int SlavePR[10] = {};

const int MOTOR=9; //Motor on Digital Pin 9

void HeartOfStainlessSteel_20x4_27() {
lcd20x4_27.init();
lcd20x4_27.backlight();

lcd20x4_27.setCursor(3, 0); // 4th column 1st row
lcd20x4_27.print("Welcome aboard");

lcd20x4_27.setCursor(8, 1); // 9th column 2nd row
lcd20x4_27.print("the");

lcd20x4_27.setCursor(6, 2); // 7th column 3rd row
lcd20x4_27.print("Heart of");

lcd20x4_27.setCursor(2, 3); // 3rd column 4th row
lcd20x4_27.print("Stainless Steel");
}

void LCD16x2_23() {
lcd16x2_23.init();
lcd16x2_23.backlight();

//PWM Simple Motor System

/*
//LCD I2C 160X2_23

lcd16x2_23.setCursor(3, 0); // 4th column 1st row
lcd16x2_23.print("I am 0x23");
//LCD I2C 160X2_23
*/
}

void I2C_Begin() {
// I2C Scanner Code
Serial.begin(9600);
while (!Serial); // wait for serial monitor
Serial.println("\nI2C Scanner");
// I2C Scanner Code
}

void I2C_Find_Address() {
// I2C Scanner Code
byte error, address;
int nDevices;

Serial.println("Scanning...");

nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();

if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");

nDevices++;
}
else if (error==4)
{
Serial.print("Unknown error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");

delay(5000); // wait 5 seconds for next scan
// I2C Scanner Code
}

void setup() {

//PWM Simple Motor System
pinMode(9,OUTPUT);

// comment out to stop the I2C find address
//I2C_Begin();
// comment out to stop the I2C find address

// These control the LCD displays
LCD16x2_23();
HeartOfStainlessSteel_20x4_27();

// I2C counter for LEDs
pinMode (LED, OUTPUT);
// Start the I2C Bus as Master
Wire.begin();
Serial.begin(9600);
// I2C counter for LEDs
}

void loop() {
//PWM Simple Motor System

for (int i=0; i<256; i++)
{
Serial.println(i);

analogWrite(MOTOR, i);
delay(10);
}
delay(500);

for (int i=255; i>=0; i--)
{
Serial.println(i);
analogWrite(MOTOR,i);
delay(10);
}

analogWrite(MOTOR,0);
delay(4000);

//Slave 1: display Photoresistor to LCD 16x2
Wire.requestFrom(1, 4);
SlavePR[1] = Wire.read();
// SlavePR[1] = map(SlavePR[1], 0, 255, 0, 1023);
// ValueA0 = map(ValueA0, 0, 1023, 0, 255);

Serial.print(SlavePR[1]);
lcd16x2_23.setCursor(0, 0);
lcd16x2_23.print("PR1:");
lcd16x2_23.setCursor(4, 0);
lcd16x2_23.print(SlavePR[1]);
lcd16x2_23.print(" ");
Serial.print("\n");

//Slave 2: display Photoresistor to LCD 16x2
Wire.requestFrom(2, 4);
SlavePR[2] = Wire.read();
Serial.print(SlavePR[2]);
lcd16x2_23.setCursor(9, 0);
lcd16x2_23.print("PR2:");
lcd16x2_23.setCursor(13, 0);
lcd16x2_23.print(SlavePR[2]);
lcd16x2_23.print(" ");
Serial.print("\n");

//Slave 3: display Photoresistor to LCD 16x2
Wire.requestFrom(3, 4);
SlavePR[3] = Wire.read();
Serial.print(SlavePR[3]);
lcd16x2_23.setCursor(0, 1);
lcd16x2_23.print("PR3:");
lcd16x2_23.setCursor(4, 1);
lcd16x2_23.print(SlavePR[3]);
lcd16x2_23.print(" ");
Serial.print("\n");

//Slave 4: display Photoresistor to LCD 16x2
Wire.requestFrom(4, 4);
SlavePR[4] = Wire.read();
Serial.print(SlavePR[4]);
lcd16x2_23.setCursor(9, 1);
lcd16x2_23.print("PR4:");
lcd16x2_23.setCursor(13, 1);
lcd16x2_23.print(SlavePR[4]);
lcd16x2_23.print(" ");
Serial.print("\n");

// comment out to stop the I2C find address
// I2C_Find_Address();
// comment out to stop the I2C find address

// I2C counter for LEDs
x++; // Increment x

//From Left to Right
// First UNO
Wire.beginTransmission(1); // transmit to device #1
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting

//Second UNO
Wire.beginTransmission(2); // transmit to device #2
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting

//Mega in the Middle

//Third UNO
Wire.beginTransmission(3); // transmit to device #3
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting

//Fourth UNO
Wire.beginTransmission(4); // transmit to device #4
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting

if ( (x == 3) || (x == 6)) {
digitalWrite(LED, HIGH);
}
else {
digitalWrite(LED, LOW);
}

if (x == 7) x = 0; // `reset x once it gets 6
delay(1000);
// I2C counter for LEDs

}