2018/12/20

DIY H Bridge Shield for Arduino Uno

This post is about the design of a DIY H Bridge Shield for Arduino Uno.

Schematic





Gerber

Top Layer


Bottom Layer


Arduino Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 *      Hall effect sensor section for filament winder  
 *      Connect the output pin (pin 3) of the hall effect sensor to Arduino digital pin 10
 */

#define Hall_Sensor 10
int current_state, previous_state;

unsigned long previousMillis = 0;
unsigned long currentMillis = 0;

int l = 0;
//---------------------------------------------------------------------------------------
/*
 *      LOW -> Turn Off MOSFET, HIGH -> Turn On MOSFET
 * 
 *      Direction (look out from the terminal end of the motor toward the shaft)
 * 
 *      AH    AL    BH    BL        MOTOR
 *      ----------------------------------
 *      HIGH  LOW   LOW   HIGH      CCW
 *      LOW   HIGH  HIGH  LOW       CW
 * 
 *      Allow 0.5 seconds delay for direction change to avoid shoot through
 *      
 *      Use high side (AH, BH) to control direction, use low side (AL, BL) for PWM
 * 
 */

#define AH 2              // A-Side P-Channel MOSFET
#define AL 5              // A-Side N-Channel MOSFET, PWM
#define BH 3              // B-Side P-Channel MOSFET
#define BL 6              // B-Side N-Channel MOSFET, PWM

#define DIR_PIN_A 8       // Direction control pin, 0 -> CCW, 1 -> CW
#define DIR_PIN_B 9

int temp_dir_a = 1;
int temp_dir_b = 1;
int dir = 0;              // direction: 0 -> CCW, 1 -> CW
int previous_dir = 0;     // previous direction
                          // if there is direction change, need to wait 1 second before making the change (for shoot through prevention)

int trigger = 0;

unsigned long startMillis = 0;
unsigned long endMillis = 0 ;

//-------------------------------------------------------------------------------------------------------------------------------------
int spool_width = 64;     // width of the spool wheel for holding the filament
float filament_diameter = 2.85;
int travel_time = 8320;   
//int interval = 370;       // travel_time / (64 / 2.85)
int interval = travel_time / spool_width * filament_diameter;

unsigned long m_startMillis = 0;

//----------------------------------------------------------------------------------------------------------------------------------------

void setup() {
     Serial.begin(9600);

     pinMode(Hall_Sensor, INPUT); // Set Arduino pin as input for hall effect sensor input

     pinMode(AH, OUTPUT);
     digitalWrite(AH, LOW); 
     
     pinMode(AL, OUTPUT);
     digitalWrite(AL, LOW);     
     
     pinMode(BH, OUTPUT);
     digitalWrite(BH, LOW);      
     
     pinMode(BL, OUTPUT);
     digitalWrite(BL, LOW);

     pinMode(DIR_PIN_A, INPUT_PULLUP);
     pinMode(DIR_PIN_B, INPUT_PULLUP);   
  
}

void loop() {

   previousMillis = currentMillis;
   
   previous_state = current_state;
   current_state = digitalRead(Hall_Sensor);
   
   if ((current_state == LOW) && (previous_state == HIGH)) {
       Serial.println("Magnet Detected");
         
//       l = l + 1;
//       Serial.print("Loop:");
//       Serial.println(l);
              
       currentMillis = millis();
//       Serial.print("Time:");
//       Serial.println(currentMillis - previousMillis);  //1000 milliseconds = 1 second

       move_spreader();
   } 
   
}

// The function for moving the filament spreader

void move_spreader() {
    
  temp_dir_a = digitalRead(DIR_PIN_A);      // Normal Open, LOW = Switch Triggered
  temp_dir_b = digitalRead(DIR_PIN_B);      // Normal Open, LOW = Switch Triggered

  if (temp_dir_a == LOW) {
    dir = 0;
    Serial.println("Switch A Trigger, dir = LOW");
//    trigger = trigger + 1;
//    Serial.print("Loop = ");
//    Serial.println((trigger / 2));
//    startMillis = millis();    
  }
  if (temp_dir_b == LOW) {
    dir = 1;
    Serial.println("Switch B Triggered, dir = HIGH");
//    trigger = trigger + 1;
//    Serial.print("Loop = ");
//    Serial.println((trigger / 2));

//    endMillis = millis();
//    Serial.print("Interval = ");
//    Serial.println((endMillis - startMillis));   
  }
  
  if (dir != previous_dir) {
      previous_dir = dir;
      digitalWrite(AH, LOW); 
      digitalWrite(BL, LOW);           
      digitalWrite(BH, LOW); 
      digitalWrite(AL, LOW); 
      delay(1000);               // delay 1 second before changing motor direction, for shoot through prevention

      //Gradualy increase motor power to reduce current spike
      if (dir == HIGH) {
          digitalWrite(AH, HIGH);         // Enable the P-channel MOSFET
          delay(5);                       // Wait for a while
          Serial.println("DIR Changed, New dir = HIGH");
          
          m_startMillis = millis();        
          for (int i = 30; i < 255; i++) {
              if ((millis() - m_startMillis) <= interval) {
                 analogWrite(BL, i);
                 delay(5);
              } else {
                analogWrite(BL, 0);
                delay(5);
              }               
          }
      } else {
         digitalWrite(BH, HIGH);          // Enable the P-channel MOSFET
         delay(5);                        // Wait for a while
         Serial.println("DIR Changed, New dir = LOW");
                   
         m_startMillis = millis();         
         for (int i = 30; i < 255; i++) {
             if ((millis() - m_startMillis) <= interval) { 
                 analogWrite(AL, i);
                 delay(5);
             } else {
                analogWrite(AL, 0);
                delay(5);
             }
      }
    }
  } else {
  
  if (dir == HIGH) {                     // Switch B Trigger, dir = HIGH
      digitalWrite(AH, HIGH);            // Enable the P-channel MOSFET
      delay(5);                          // Wait for a while
      Serial.println("Same Direction, dir = HIGH");
      
      m_startMillis = millis();
      for (int i = 30; i < 255; i++) {
              if ((millis() - m_startMillis) <= interval) {
                 analogWrite(BL, i);
                 delay(5);
              } else {
                analogWrite(BL, 0);
                //delay(5);
              }               
          }
      // analogWrite(BL, 255);              // Enable the N-channel MOSFET, Min. 45, The motor will start turning
             
  } else {
      digitalWrite(BH, HIGH);            // Enable the P-channel MOSFET 
      delay(5);                          // Wait for a while 
      Serial.println("Same Direction, dir = LOW");
      
      m_startMillis = millis();
      for (int i = 30; i < 255; i++) {
              if ((millis() - m_startMillis) <= interval) {
                 analogWrite(AL, i);
                 delay(5);
              } else {
                analogWrite(AL, 0);
                //delay(5);
              }               
          }
      
      //analogWrite(AL, 255);              // Enable the N-channel MOSFET, Min. 45, The motor will start turning 
  }
  }
}

The Results





No comments:

Post a Comment