Ruslan_nig

simple Hbridge and PWM example

Oct 29th, 2023
2,040
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //this is my example for demonstration H-bridge on n-p-n transistors (BJT - Bipolar Junction Transistors) and for PWM
  2. //simpleGo function is for H-bridge presentation
  3. //PWMmotor - simple PWM example
  4. //this code placed on Pastebin
  5. uint8_t pwm = 55;
  6. uint8_t step = 1;
  7. void setup() {
  8.   pinMode(6, OUTPUT);
  9.   pinMode(3, OUTPUT);
  10.   Serial.begin(9600);
  11. }
  12.  
  13. void simpleGo(){
  14.   digitalWrite(6, 1);
  15.   digitalWrite(3, 0);
  16.   delay(2000);
  17.   digitalWrite(6, 0);
  18.   digitalWrite(3, 0);
  19.   delay(2000);
  20.   digitalWrite(6, 0);
  21.   digitalWrite(3, 1);
  22.   delay(2000);
  23.   digitalWrite(6, 0);
  24.   digitalWrite(3, 0);
  25.   delay(2000);
  26. }
  27. void PWMmotor(){
  28. while(pwm<=254) {
  29.     analogWrite(6, pwm);
  30.     delay(100);
  31.     pwm  = pwm + step;
  32.     Serial.print("INCREASE");
  33.     Serial.println(pwm);
  34. }
  35.  
  36. while(pwm>=55) {
  37.     analogWrite(6, pwm);
  38.     delay(100);
  39.     pwm  = pwm - step;
  40.     Serial.print("decrease decrease decresase");
  41.     Serial.println(pwm);
  42. }
  43.  
  44. }
  45.  
  46. void loop() {
  47. //simpleGo();
  48. //PWMmotor();
  49. analogWrite(6,120);
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment