GPIO pins no errors but not getting the expected result? - python

Sorry I am a beginner to both Raspberry Pi and python. I am write a simple python program to use the pulse width modulator, Here is the code.
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(12,GPIO.OUT)
pwm=GPIO.PWM(12,50)
pwm.start(0)
while True:
for i in range(50):
pwm.ChangeDutyCycle(i)
time.sleep(0.05)
for i in range(50,0,-1):
pwm.ChangeDutyCycle(i)
time.sleep(0.05)
I connected the led between 12 and ground with the suitable resistor. But when I execute, I don't get any error but it wont work.

The code seems to work fine on my Rpi-3. So we cannot help you if we can't see the circuit design. I can make a guess that you probably used the normal numbering to connect the led, but used the BCM numbering in your program. So either change it to board numbering by GPIO.setmode(GPIO.BOARD) or refer the below chart for correct numbers.

I got the problem. I need to use the proper numbering. The reason for having two types of numbering is,
The GPIO.BOARD option specifies that you are referring to the pins by the number of the pin the the plug - i.e the numbers printed on the board (e.g. P1).
The GPIO.BCM option means that you are referring to the pins by the "Broadcom SOC channel" number.
I hope this helps anyone.

Related

Building dummy package using pyCharm

I am writing a GUI for my Raspberry pi, but I'd like to work on the code on PC, then drop it into the pi, just cos the tools are better and I mostly have remote access to the pi etc etc. My problem is that the GUI does some calls to change IO pins on the Pi, using the RPi.GPIO library, which doesn't exist on PC. Of course I can comment out the lines of pi-specific code, but that's really messy especially if I start going back and forth. My idea is to set up a dummy/mirror library for the PC, then the code picks the dummy library on PC and the real library on the pi. Seems simple, but I'm getting really bogged down creating my own library. So, to my question, in summary - what is the easiest way to create a quick library using PyCharm that my code would pick up...
Here is some quick code for context...
import RPi.GPIO as GPIO # this is the library I want to mirror
# sets pin numbering on pi, does completely nothing in
#  the dummy I want to call in on on PC
GPIO.setmode(GPIO.BOARD)
GPIO.output(self.reset_pin, 1) # also does stuff on pi, nothing on PC
There is the fake-rpi package on pypi:
So, does this simulate everything on a Raspberry Pi? No! Right now it simulates what I use and need. Over time, more will be added. You are also welcome to submit pull requests for things I haven't added yet.
But it looks like it simulates GPIO pins

Delays between servo movements in pyfirmata don't work

I'm writing a program in python where moving servos at specific times is a crucial part of the project. To achive this, I am using the pyfirmata library.
I've tried two methods of delays, but none of them seem to work.
When I run the code, the servo turns the first time, but after the delay, it doesn't turn and the program just stops, instead of moving the servo to 0 degrees and then stopping.
This is the one with the board.pass_time() built in to the pyfirmata library:
from pyfirmata import Arduino, util
import time
board = Arduino('COM3')
servo = board.get_pin('d:9:s')
servo.write(180) #This works and turns the servo
time.sleep(1)
servo.write(0) #This time the servo does not turn, then the program ends
And here's the one with time.sleep():
from pyfirmata import Arduino, util
board = Arduino('COM3')
servo = board.get_pin('d:9:s')
servo.write(180) #This works and turns the servo
board.pass_time(1)
servo.write(0) #This time the servo does not turn, then the program ends
I would highly appreciate if someone could help.
Thank you,
David
I found the answer!
There might be a better answer, but if I write board.get_pin('d:9:s') as board.get_pin('d:9:'), like this, I need to control it with a value from 0 to 5 instead of 0 to 360, but the delays work
Your code is correct but you need to make the delay again to turn it back to 0 degree. So your code should look something like this.
servo.write(180)
time.sleep(2)
servo.write(0)
time.sleep(2)
You need to give the delay everytime you make changes to the servo motor in order to make those changes

Start Loop when input start from a PI and stop when input stops (python)

Python: I'm writing a system that needs the program to wait until an input from the GPIO board on a raspberry comes through then loop until the input stops? Any ideas on the best way to do this?
The best way , imho, is to use the event_detected() function of the RPI.GPIO module.
Take a look at http://raspi.tv/2014/rpi-gpio-update-and-detecting-both-rising-and-falling-edges
The example program detects rising and falling edges on GPIO 25

Interrupts using GPIO and python 3

I have a raspberry pi and I was wondering if there was a way to interrupt a program using a GPIO pin? For example, the pi begins printing a long story but if i click a button and close the switch it will pause and turn on a led. I did some research but i could only find stuff for python 2 while I am using 3. Thank you any help is greatly appreciated. :D
The RPi.GPIO library supports Python 3, so you can start by using that.
Assuming that your RPi board is one that uses GPIO 3, here is an example:
import RPi
RPi.GPIO.setmode(RPi.GPIO.BCM)
RPi.GPIO.output(3, RPi.GPIO.LOW)

Find Status of LED on coffee maker using Arduino?

I'm new to the Arduino platform and i'm wondering if it is possible to tell if an LED is on using an arduino input? I'm confused as to if i can check the voltage of one side of the LED in reference to the Arduino's ground? or would i have to check the voltage relative to the other terminal of the LED.
Just a little confused.
This question is probably better asked on electronics.SE, but here goes:
Yes, it is possible, and there are a couple of ways you could do this:
You could hook up a light sensor to your arduino, aim it at the led, and read that. No modification of the coffee maker is needed
You could connect a wire to a coffee maker LED, and connect it to Arduino. But where to connect the wire? To understand this you have to consider how LEDs are usually hooked up: there is the control signal, the LED itself, a current-limiting diode, and ground. The two main options look like this:
Assuming that the control voltage is either +5 or zero volts, and assuming your arduino runs on that (and not 3.3V, for example), in both cases (the one on the left and the one on the right), you want to connect your wire to the point indicated as "control". In the left hand case is "before" the resistor, but in the right hand case it is "before" the LED. You should use your multimeter to confirm your assumptions about the LED circuit, and verify that the connection point indeed is either at +5V and 0v (or whatever your expected voltages are) when the LED is on or off, respectively (if you don't have a multimeter, then this might not be a good project for you)

Categories

Resources