Raspberry pie for mobile photos

  • 2021-06-29 11:37:24
  • OfStack

The intermediate library functions that drive raspberry pie gpio are wiringPi, BCM2835, and PRi.GPIO. Here I choose PRi.GPIO developed in Python.

1. Install RPi.GPIO

(1) Install python-dev first, enter the following instructions.


sudo apt-get install python-dev

(2) Install RPi.GPIO


wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.11.tar.gz
# Unzip: 
tar -zxvf RPi.GPIO-0.5.11.tar.gz
# Enter the unzipped directory   : 
cd RPi.GPIO-0.5.3a
# Start Installation   : 
sudo python setup.py install

2. Program for driving motor

(1) Connection method between raspberry pie and XY-160D drive plate


#  Raspberry pie powered: 
# 5V Power Positive to Raspberry Pie 4 Number 5V Power Pin, Negative to Raspberry Pie 39 Number GND Pin 
#  Raspberry pie green ATC Flash rule SD Card is running, red PWR Normal power supply when the lamp is on 
#  For more indicator status see : raspberry pi LED Interpretation of light status 

#
#  Power supply to the power end of the drive plate: 
# 14.8V Power connector, positive end, negative end 
#
#  Drive board signal end power supply: 
#  raspberry pi 1 Pin number 3.3V Connect to drive board signal end +5V Pin, 14 Pin number GND Connect to drive board signal end GND Pin 
#  Normal Rules for Power Supply of Drive Board DS1 Indicator light on 

(2) Code

Test with a simple motor-driven code to create a new motor_drive.py file, add the following code and save:


#!/usr/bin/python2
#coding=utf-8
import RPi.GPIO as GPIO
import time
 
#  Initialize Set Pin Output 
IN1 = 12     #11 Pin to pin GPIO0,12 Pin to pin GPIO1
IN2 = 11     #11 Number and 12 Pin number to drive plate IN2 , IN1 Pin, control it 1 Side motor 
IN3 = 13     #13 Pin to pin GPIO2,15 Pin to pin GPIO3
IN4 = 15     #13 Number and 15 Pin number to drive plate IN3 , IN4 Pin, control another 1 Side motor 
def init():
  GPIO.setmode(GPIO.BOARD) # Set up GPIO Code as physical pin BOARD Code 
  GPIO.setup(IN1, GPIO.OUT)
  GPIO.setup(IN2, GPIO.OUT)
  GPIO.setup(IN3, GPIO.OUT)
  GPIO.setup(IN4, GPIO.OUT)
 
#  All pin position low level for reset, stop function 
def reset():
  GPIO.output(IN1, GPIO.LOW)
  GPIO.output(IN2, GPIO.LOW)
  GPIO.output(IN3, GPIO.LOW)
  GPIO.output(IN4, GPIO.LOW)
  
def forward(): 
  GPIO.output(IN1, GPIO.HIGH)
  GPIO.output(IN2, GPIO.LOW)
  GPIO.output(IN3, GPIO.HIGH)
  GPIO.output(IN4, GPIO.LOW)
def back():
  GPIO.output(IN1, GPIO.LOW)
  GPIO.output(IN2, GPIO.HIGH)
  GPIO.output(IN3, GPIO.LOW)
  GPIO.output(IN4, GPIO.HIGH)
 
def stop():
  reset()
if __name__ == "__main__":
  init()
  reset()
  try:
   while True:
    forward()
    time.sleep(5)
    stop()
    time.sleep(1)    
    back()
    time.sleep(1)
    stop()
    time.sleep(1)
  except KeyboardInterrupt:
   print("except")
   stop()
  GPIO.cleanup()

Execute the program and press Ctrl + c to end the program


sudo python motor_drive.py

Note:

(1)#!/usr/bin/env python, defines the absolute path of the python parsing script.
(2) # -*- coding: utf-8 -*-, python file is in utf-8 format, otherwise Chinese comments cannot be written.
(3) GPIO.setmode (GPIO.BOARD), numbered by board.
(4) The python program uses the try except language. When Ctrl+C is pressed to end the program, an exception is triggered. The program executes the gpio.cleanup() statement to clarify the GPIO pin status.


Related articles: