I'm doing a project and I am using an HC-06, raspberry PICO, 28BYJ-48 Motor, and a ULN2003 Motor Driver. I coded the motor to turn and everything, and it all works perfectly. I debugged, and the Micropython interpreter shows no errors. I am using an app with it through my HC-06, and for some reason when everything is connected and my Bluetooth module shows as "Connected" on the app, it appears that the motor is not turning. Does anyone know what could be wrong? I am fairly new to Micropython. Here is my code:
from machine import Pin, Timer
from machine import UART
from utime import sleep
uart = UART(0, 9600)
number_of_steps = 1
max_steps = 10
pins = [
Pin(2, Pin.OUT),
Pin(3, Pin.OUT),
Pin(4, Pin.OUT),
Pin(5, Pin.OUT),
]
full_step_sequence = [
[1,0,0,0],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1]
]
back_step_sequence = [
[0,0,0,1],
[0,0,1,0],
[0,1,0,0],
[1,0,0,0]
]
def rotate():
for step in full_step_sequence:
for i in range (len(pins)):
pins[i].value (step[i])
sleep(0.001)
def rotate_back():
for step in back_step_sequence:
for i in range (len(pins)):
pins[i].value (step[i])
sleep(0.001)
while True:
if uart.any():
data=uart.read()
data = str(data)
print(data)
if ('MUTE' in data):
rotate()
elif ('UNMUTE' in data):
rotate_back()
Related
I have a Raspberry Pi that is connected to a INA219 current sensor that currently runs a python code that measures the Bus voltage, bus current, power, and shunt voltage of the Raspberry pi and stores it within a CSV file that runs 10 times per second until the user stops it (CTRL + C). I am trying to create a code on my Arduino so that the code on the raspberry pi can run on a separate device, which is the Arduino UNO. The code from the raspberry pi that I am trying to get to run on a separate device is attached below:
#Importing libraries
import csv
import time
from ina219 import INA219
from ina219 import DeviceRangeError
SHUNT_OHMS = 0.1
read_ina = INA219(SHUNT_OHMS)
read_ina.configure()
def read_all():
data = {}
data['Bus Voltage'] = read_ina.voltage()
data['Bus Current'] = read_ina.current()
data['Power'] = read_ina.power()
data['Shunt Voltage'] = read_ina.shunt_voltage()
data ['Frequency'] = 0
return data
with open('SensorData.csv', 'w') as f:
data = read_all()
writer = csv.DictWriter(f,
fieldnames = list (data.keys()))
writer.writeheader()
exit = False
while not exit:
try:
writer.writerow(data)
start_time = time.perf_counter()
data = read_all()
compute_time = time.perf_counter() - start_time
data['Frequency'] = 1 / compute_time
except KeyboardInterrupt:
exit = True
Again, I am having a hard time with having it show on the Arduino UNO interface. I have the Arduino IDE installed on my Raspberry pi and have not had luck with such a code.
I've read many of the questions here on SO, but none of the solutions seem to solve my problem. I have a file called "light_led.py" which looks like this
import RPi.GPIO as gpio
import time
# Set the mode for the GPIO numbering
gpio.setmode(gpio.BCM)
pin = 23
# Set the GPIO as output
gpio.setup(pin, gpio.OUT, initial=gpio.LOW)
try:
while True: # Run forever
gpio.output(pin, gpio.HIGH) # Turn on
time.sleep(1) # Sleep for 1 second
gpio.output(pin, gpio.LOW) # Turn off
time.sleep(1) # Sleep for 1 second
except KeyboardInterrupt:
# cleanup all GPIO
gpio.cleanup()
print('Cleaned up')
It simply toggles an LED on and off. This file is called in the following script
import RPi.GPIO as gpio
import time
import subprocess
import signal
# Set the mode for the GPIO numbering
gpio.setmode(gpio.BCM)
pin = 24
# Set the GPIO as input w/ pull up
gpio.setup(pin, gpio.IN, pull_up_down = gpio.PUD_UP)
# Initialize
pin_prev = gpio.input(pin)
while True:
pin = gpio.input(pin)
if (pin == 0) and (pin_prev == 1):
process = subprocess.Popen('python <PATH-TO-FILE>/light_led.py', shell=True)
print('Should be blinking')
if (pin == 1) and (pin_prev == 0):
process.send_signal(signal.SIGINT)
print('Should be off')
# Update previous state
pin_prev = pin
# Slight pause to debounce
time.sleep(0.05)
The files run fine and do what they're suppose to. The only problem I'm having is that I can't stop the blinking light by "flipping the switch". In other words, I'm having trouble with this process.send_signal(signal.SIGINT). There are not errors, so I don't have much to report.
I've tried process.terminate() and os.kill(process.pid, signal.SIGINT), both with no luck. I just want to stop the subprocess from running and continue with the main toggle script. From what I read, "signal.SIGINT" should invoke the "KeyboardInterrupt" in the subprocess...but maybe not?
I'm testing my COM port cuz I need to read streaming data from a radar board by python.
First, I wrote a arduino code like below figure.
It generate number continuously.
And wrote the pyserial:
import serial
import time, os
ser = serial.Serial('COM6', 9600, timeout=0, parity=serial.PARITY_ODD, rtscts=0)
if (ser.isOpen() == True):
print ("Port:",ser.port)
#input = input(">> ")
#input = b"p"
ser.write(b'1\n\r')
time.sleep(1)
numOfLines = 0
while 1:
#if (ser.inWaiting()>0):
response = ser.readline()
print (response)
numOfLines +=1
if numOfLines>10:
break
ser.close()
But I got the result:
If I run the arduino serial output window before I compile my python code, I will get the output correctly.(like b'193\n', b'194\n'...), or I'll get nothing.(b'').
Can someone tell me what's wrong with my processing?
I am working with the SainSmart 16-Channel Relay board and everything is working fine except for one pin is sending a different voltage. This is causing the relay to shutoff after a few seconds. All the pins show 3.3v except for pin 4 that is showing 4.25v when I test the terminal with my multi-meter. I have added a 1k resister inline with the GPIO pin because the board is designed for Arduino and not the Raspberry Pi. The resister has corrected the voltage difference issue because 15 other relays work fine. Also I am reading this voltage issue before the 1k resistor so it should be effecting this.
I have changed out the Raspberry Pi board and the relay board with another thinking that was the issue and am receiving the exact same voltage. Any ideas as to what the issue could be would be greatly appreciated.
Below is the code I am using to test pins 2-8.
from tkinter import *
import RPi.GPIO as GPIO
class Create_Button:
def __init__(self,pin):
self.pin = pin
GPIO.setup(self.pin, GPIO.OUT)
GPIO.output(self.pin, GPIO.HIGH)
self.b = Button(text="Pin %s" % self.pin + " - Turn Relay On", command=self.toggle)
self.b.pack()
def toggle(self):
print(self.pin)
if GPIO.input(self.pin):
GPIO.output(self.pin, GPIO.LOW)
self.b.config(text="Pin %s" % self.pin + " - Turn Relay On")
else:
GPIO.output(self.pin, GPIO.HIGH)
self.b.config(text="Pin %s" % self.pin + " - Turn Relay Off")
root = Tk()
root.title("Toggler")
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(FALSE)
for pin_count in range(2,9+1):
button = Create_Button(pin_count)
quitButton = Button(root, text="Quit", command=exit)
quitButton.pack(side=LEFT)
root.mainloop()
GPIO.cleanup()
As part of an interactive installation, I would like to program an audio filter LPF (cut high frequencies) on a sound that runs in a loop on python. In addition I would like to vary the cutoff frequency of the filter according to the distance of an obstacle measured by a distance sensor, all on a Raspberry Pi 3.
I have already managed to program the distance measurement by the sensor and to run a looping sound but I do not know how to do it for the audio filter. By searching on the internet I can not find an answer and a clear method on my situation.
Here is my code:
#Libraries
import RPi.GPIO as GPIO
import time
import pygame
pygame.init()
caca = pygame.mixer.Sound("bells001.wav")
pygame.mixer.music.load("bells001.wav")
#GPIO Mode (BOARD / BCM)
GPIO.setmode(GPIO.BCM)
pygame.mixer.music.play(-1)
#set GPIO Pins
GPIO_TRIGGER = 23
GPIO_ECHO = 24
#set GPIO direction (IN / OUT)
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
def distance():
# set Trigger to HIGH
GPIO.output(GPIO_TRIGGER, True)
# set Trigger after 0.01ms to LOW
time.sleep(0.00001)
GPIO.output(GPIO_TRIGGER, False)
StartTime = time.time()
StopTime = time.time()
# save StartTime
while GPIO.input(GPIO_ECHO) == 0:
StartTime = time.time()
# save time of arrival
while GPIO.input(GPIO_ECHO) == 1:
StopTime = time.time()
# time difference between start and arrival
TimeElapsed = StopTime - StartTime
# multiply with the sonic speed (34300 cm/s)
# and divide by 2, because there and back
distance = (TimeElapsed * 34300) / 2
return distance
if __name__ == '__main__':
try:
while True:
dist = distance()
print (dist)
time.sleep(0.2)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")
GPIO.cleanup()
I tried this for now
from pydub import AudioSegment
from pydub.playback import play
song = AudioSegment.from_wav("bells001.wav")
new = song.low_pass_filter(10000)
play(new)
Unfortunately instead of me out a filtered sound, I have the sound very degraded with lots of crunches ... Anyone have an idea?
I tried other scripts but I still have problems with module or library not found while I install and reinstall all these modules.
Can anyone help me?