Software

Python Code for Testing Ultra Sonic Distance Sensor:

from gpiozero import DistanceSensor
from time import sleep
sensor = DistanceSensor(echo=18, trigger=17)
while True:
    print('Distance: ', sensor.distance * 100)
    sleep(1)

Source: https://gpiozero.readthedocs.io/en/stable/api_input.html

Python Code for Testing Servo Motor:

from gpiozero import AngularServo
>>> s = AngularServo(17, min_angle=-42, max_angle=44)
>>> s.angle = 0.0
>>> s.angle
0.0

Reference: https://gpiozero.readthedocs.io/en/stable/api_output.html

Python Code for Testing Servo Motor in every 10 degrees repeating with the code above as a reference:

from gpiozero import AngularServo
from time import sleep

servo=AngularServo(17)
def my_range(start, end, step):
      while start <= end:
             yield start
             start += step
def decrement_range(start, end, step):
        while start >= end:
              yield start
              start += step
while True:
      for x in my_range(-90.0, 90.0, 10.0):
            servo.angle =x
             sleep(0.1)
      for x in decrement_range(90.0, -90.0, -10.0):
             servo.angle=x
             sleep(0.1)

References:

https://gpiozero.readthedocs.io/en/stable/api_output.html

https://wiki.python.org/moin/ForLoop

C++ Code for Testing DC Motor:

// Defining the pulse width modulator pins on Main Drive Circuit Board with Arduino pins
#define d0 2
#define d1 3
#define d2 4
#define d3 5
#define d4 6
#define d5 7
#define d6 8
#define dir 9

void setup() {
  // setting the pulse width modulator pins on Main Drive Circuit Board as outputs:
  pinMode(d0, OUTPUT);
  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(d5, OUTPUT);
  pinMode(d6, OUTPUT);
  pinMode(dir, OUTPUT);
  digitalWrite(d0, LOW);
  digitalWrite(d1, LOW);
  digitalWrite(d2, LOW);
  digitalWrite(d3, LOW);
  digitalWrite(d4, LOW);
  digitalWrite(d5, LOW);
  digitalWrite(d6, LOW);
  digitalWrite(dir, LOW);
  Serial.begin(9600);
}
void loop() {
  // put your main code here, to run repeatedly:
  boolean isValidInput;
  // draw a menu on the serial port
  Serial.println( "-----------------------------" );
  Serial.println( "MENU:" );
  Serial.println( "1) Fast forward" );
  Serial.println( "2) Forward" );
  Serial.println( "3) Soft stop (coast)" );
  Serial.println( "4) Reverse" );
  Serial.println( "5) Fast reverse" );
  Serial.println( "6) Hard stop (brake)" );
  Serial.println( "-----------------------------" );
  do
  {
    byte c;
    // get the next character from the serial port
    Serial.print( "?" );
    while( !Serial.available() )
      ; // LOOP...
    c = Serial.read();
    // execute the menu option based on the character recieved
    switch( c )
    {
      case '1': // 1) Fast forward
        Serial.println( "Fast forward..." );
        // always stop motors briefly before abrupt changes
        digitalWrite(d0, HIGH);
        digitalWrite(d1, HIGH);
        digitalWrite(d2, HIGH);
        digitalWrite(d3, HIGH);
        digitalWrite(d4, HIGH);
        digitalWrite(d5, HIGH);
        digitalWrite(d6, HIGH);
        digitalWrite(dir, HIGH);
        isValidInput = true;
        break;  

      case '2': // 2) Forward   
        Serial.println( "Forward..." );
        // always stop motors briefly before abrupt changes
        digitalWrite(d0, HIGH);
        digitalWrite(d1, HIGH);
        digitalWrite(d2, HIGH);
        digitalWrite(d3, HIGH);
        digitalWrite(d4, HIGH);
        digitalWrite(d5, LOW);
        digitalWrite(d6, HIGH);
        digitalWrite(dir, HIGH);
        isValidInput = true;
        break;  

      case '3': // 3) Soft stop (preferred)
        Serial.println( "Soft stop (coast)..." );
        digitalWrite(d0, LOW);
        digitalWrite(d1, LOW);
        digitalWrite(d2, LOW);
        digitalWrite(d3, LOW);
        digitalWrite(d4, LOW);
        digitalWrite(d5, LOW);
        digitalWrite(d6, LOW);
        digitalWrite(dir, LOW);
        isValidInput = true;
        break;  
      case '4': // 4) Reverse
        Serial.println( "Reverse..." );
        // always stop motors briefly before abrupt changes
        digitalWrite(d0, HIGH);
        digitalWrite(d1, HIGH);
        digitalWrite(d2, HIGH);
        digitalWrite(d3, HIGH);
        digitalWrite(d4, HIGH);
        digitalWrite(d5, LOW);
        digitalWrite(d6, HIGH);
        digitalWrite(dir, LOW);
        isValidInput = true;
        break;  

      case '5': // 5) Fast reverse
        Serial.println( "Fast reverse..." );
        // always stop motors briefly before abrupt changes
        digitalWrite(d0, HIGH);
        digitalWrite(d1, HIGH);
        digitalWrite(d2, HIGH);
        digitalWrite(d3, HIGH);
        digitalWrite(d4, HIGH);
        digitalWrite(d5, HIGH);
        digitalWrite(d6, HIGH);
        digitalWrite(dir, LOW);
        isValidInput = true;
        break;

      case '6': // 6) Hard stop (use with caution)
        Serial.println( "Hard stop (brake)..." );
        digitalWrite(d0, LOW);
        digitalWrite(d1, LOW);
        digitalWrite(d2, LOW);
        digitalWrite(d3, LOW);
        digitalWrite(d4, LOW);
        digitalWrite(d5, LOW);
        digitalWrite(d6,  LOW);
        digitalWrite(dir, HIGH);
        isValidInput = true;
        break;  

      default:
        // wrong character! display the menu again!
        isValidInput = false;
        break;
    }
  } while( isValidInput == true );
  // repeat the main loop and redraw the menu...
}

Reference: https://www.bananarobotics.com/shop/How-to-use-the-HG7881-(L9110)-Dual-Channel-Motor-Driver-Module

Comments are closed.