Hello,
I want to use a L293d motor shield to drive a solenoid.
I know this has already been done before and I am trying to replicate this project. Only that I am trying to do something much simpler with only one board and one solenoid to begin with.
My solenoid is also smaller. It is home made solenoid which is only 4 mm in diameter and requires only 4.5V to operate, that I provide with an external power input. The resistance of the solenoid is about 1 ohm. So, I guess the current is about 1.1 Amp in the solenoid.
I am using the same code as the original project, except that I kept only the HARDWARE_TEST part.
During a code test run where I am calling allChannelOutput() for a long time, I measure 4.3 V with my multimeter on the shield motor pins. The solenoid works fine when I manually connect the wires to the shield outputs for a very short time.
However, when I finally connect the solenoid and call allChannelOutput() for only 100 ms (which is how the solenoid is supposed to be operated), the solenoid isn't activated. There does not seem to be enough current running through the solenoid.
Am I doing something wrong ?
Here is my code:
// Arduino pins for the shift register
#define MOTORLATCH 12
#define MOTORCLK 4
#define MOTORENABLE 7
#define MOTORDATA 8
// 8-bit bus after the 74HC595 shift register
// (not Arduino pins)
// These are used to set the direction of the bridge driver.
#define MOTOR1_A 2
#define MOTOR1_B 3
#define MOTOR2_A 1
#define MOTOR2_B 4
#define MOTOR3_A 5
#define MOTOR3_B 7
#define MOTOR4_A 0
#define MOTOR4_B 6
// Arduino pins for the PWM signals.
#define MOTOR1_PWM 11
#define MOTOR2_PWM 3
#define MOTOR3_PWM 6
#define MOTOR4_PWM 5
#define SERIAL_BAUD_RATE 57600
// Array storing the 8-bit bus after the 74HC595 shift register
uint8_t motorBus[] = { MOTOR1_A , MOTOR1_B, MOTOR2_A , MOTOR2_B,
MOTOR3_A , MOTOR3_B, MOTOR4_A , MOTOR4_B };
// Array storing the Arduino pins for the PWM signals
uint8_t motorPwmPins[] = { MOTOR1_PWM, MOTOR2_PWM, MOTOR3_PWM, MOTOR4_PWM };
// Used for Hardware Test mode (when HARDWARE_TEST = 1)
uint8_t testHardwareSolenoidCount;
uint8_t testHardwarePauseCount;
// ****************************************************************************
// setup() - Initialization Function
// ****************************************************************************
void setup()
{
// Initialize Serial communication and Slave Address
Serial.begin(SERIAL_BAUD_RATE);
// Initialize Test Mode
testHardwareSolenoidCount = 0;
}
// ****************************************************************************
// loop() - Main Program Loop Function
// ****************************************************************************
void loop()
{
// De-energize the solenoid
allChannelOutput(0);
delay(10000);
// Energize the solenoid
allChannelOutput(1 << testHardwareSolenoidCount);
delay(100);
// De-energize the solenoid
allChannelOutput(0);
delay(10000);
}
// ****************************************************************************
// allChannelOutput() - Sets the state of all channels per 'outputChannels'
// which is an 8-bit bitmask of the channels to be energized.
// ****************************************************************************
void allChannelOutput(uint8_t outputChannels)
{
uint8_t i;
uint8_t motorChannels;
// Convert the bitmask of 'outputChannels', which corresponds to the
// shield terminal channels 0 through 7, to the bitmask of the
// 8-bit bus after the 74HC595 shift register
for (i = 0; i < 8; i++)
bitWrite(motorChannels, motorBus[i], bitRead(outputChannels, i));
// Send the 8-bit bitmask to the Shift register
shiftWriteAll(motorChannels);
// Set the PWM Pins. Each PWM pin is set to 255 (fully-on) if either
// the corresponding A or B is on, and set to 0 (fully off) otherwise
for (i = 0; i < 4; i++)
{
if(bitRead(outputChannels,2*i) || bitRead(outputChannels, 2*i+1))
analogWrite(motorPwmPins[i], 255);
else
analogWrite(motorPwmPins[i], 0);
}
}
// ---------------------------------
// shiftWriteAll
//
// A variant of the shiftWrite function
// Pass it the bitMask of all channels to be written.
// There is no initialization function.
// Initialization is automatically done at the first
// time it is used.
//
void shiftWriteAll(int outputBitmask)
{
static int latch_copy;
static int shift_register_initialized = false;
// Do the initialization on the fly,
// at the first time it is used.
if (!shift_register_initialized)
{
// Set pins for shift register to output
pinMode(MOTORLATCH, OUTPUT);
pinMode(MOTORENABLE, OUTPUT);
pinMode(MOTORDATA, OUTPUT);
pinMode(MOTORCLK, OUTPUT);
// Set pins for shift register to default value (low);
digitalWrite(MOTORDATA, LOW);
digitalWrite(MOTORLATCH, LOW);
digitalWrite(MOTORCLK, LOW);
// Enable the shift register, set Enable pin Low.
digitalWrite(MOTORENABLE, LOW);
// start with all outputs (of the shift register) low
latch_copy = 0;
shift_register_initialized = true;
}
// The defines HIGH and LOW are 1 and 0.
// So this is valid.
//bitWrite(latch_copy, output, high_low);
latch_copy = outputBitmask;
// Use the default Arduino 'shiftOut()' function to
// shift the bits with the MOTORCLK as clock pulse.
// The 74HC595 shiftregister wants the MSB first.
// After that, generate a latch pulse with MOTORLATCH.
shiftOut(MOTORDATA, MOTORCLK, MSBFIRST, latch_copy);
delayMicroseconds(5); // For safety, not really needed.
digitalWrite(MOTORLATCH, HIGH);
delayMicroseconds(5); // For safety, not really needed.
digitalWrite(MOTORLATCH, LOW);
}
Attached is my super simple wiring.

