3

I'm running Raspbian Jessie on my Raspberry Pi 3 and I'm doing a little house alarm project. When the alarm is activated the Pi should transmit FM signal of an alarm sound on a frequency that a few radios in the house will be tuned at. The program I use for transmiting FM is here and it uses PWM (on GPIO 4) to transmit FM.

Transmiting audio works as expected but the problem is present on my LCD 16x2 display (it's connected to I2C). Whenever I run the LCD and transmit in the same time the LCD starts displaying gibberish (first couple of times it even raised an I/O Error 5, but I can't reproduce it anymore). All other functions of the alarm such as arming, disarming or disabling the siren are working as expected while transmiting.

The reason I think PWM conflicts with I2C is because on this post an I2C clock is mentioned, it's on GPIO 3, but that still makes me believe there are more I2C connections between the pins (even though I didn't find them in this reference)


The question:
How would I use the I2C LCD and transmit FM using the PWM method in the same time?


Extra tehnical info:

  • LCD is I2C LCD1602 from this kit
  • LCD module source can be found here. It's rather high level so it's simpler for me and my week of experience with a Raspberry Pi.
  • Last time the I/O Error was raised was at the BUS.write_byte function in the LCD module source (can't remember what line, the error doesn't occur anymore but is surely linked with the LCD problem)

EDIT:

The Error 5 happened again (file at the github link above):

Traceback (most recent call last):
  File "alarm.py", line 432, in <module>
    setup()
  File "alarm.py", line 133, in setup
    LCD1602.clear()
  File "/home/pi/alarm/LCD1602.py", line 75, in clear
    send_command(0x01) # Clear Screen
  File "/home/pi/alarm/LCD1602.py", line 21, in send_command
    write_word(LCD_ADDR ,buf)
  File "/home/pi/alarm/LCD1602.py", line 15, in write_word
    BUS.write_byte(addr ,temp)
IOError: [Errno 5] Input/output error

EDIT 2: Breadboard Breadboard 2 Connections on the LCD

There are the pictures. I don't think anything is incorrectly connected. (Sorry about the pictures being blurry, I don't have a good camera)

9
  • 1
    I2C I/O errors are usually because of poor or loose wiring.
    – joan
    Aug 13, 2016 at 7:38
  • I'll recheck my wiring again, but that shouldn't be a problem since: a) I'm using a solderless breadboard b) The LCD works fine when not activating the FM transmiter in software.
    – Ciprum
    Aug 13, 2016 at 10:42
  • Might be worth posting a photo showing the connections. I thought Pifm used GPIO18 (on pin 12). Are you sure you are using GPIO4 (on pin 7)?
    – joan
    Aug 13, 2016 at 11:03
  • I added the pictures, I'll try GPIO 18.
    – Ciprum
    Aug 13, 2016 at 12:36
  • @joan Same thing happends with GPIO 18 as with GPIO 4. Check my comment on goldilocks's answer.
    – Ciprum
    Aug 13, 2016 at 13:01

2 Answers 2

4

PWM should not conflict with I2C.

Everything below presumes the (predominant) Broadcom pin numbering scheme, which does not correlate to the physical arrangement of the pins.

I've never used GPIO 4 for PWM; the normal PWM pins used are 12, 13, 18, and 19, making use of two separate PWM channels derived from one clock.

Have a look at the chart here. Note that the crossed out pins simply indicate those which aren't available on 26-pin models. Although it contains additional information, the parts we are concerned with here are pretty much lifted directly from section 6.2 of the BCM2835 Peripherals datasheet, so they are authoritative.

Notice that the normal PWM pins I've referred to have as either their default, or include as an ALT function, PWM0 or PWM1; this refers to the two channels just mentioned. The default purpose of one of them, pin 18, is access to the PCM clock, which is used for the timing of digital audio but may be turned to other purposes.

Pin 4, which you have said you are using, is not connected to any of the above. Instead, it uses "general purpose clock 0". However, I can't fine any reason to believe this affects the I2C clock, and looking around at documentation for libraries which can make use of general purpose clock 0 via GPIO 4 and the I2C bus, there is no indication that this should be problematic.

Put another way, it sounds like you have a eccentric issue. Whether this is due to a mistake in your code or connections, some physical defect in something, or the code written by someone else you are making use of (e.g., for the FM transmission) is impossible to say, but there is thus is no meaningful answer to your question because the problem you have is not reproducible in the general sense -- the "general sense" being that if you want to use PWM and I2C simultaneously there is not a problem and the "how to do it" is to just do it.

9
  • I have tried connecting the antenna to GPIO 18 instead of 4 and the same thing happends. However what's really interesting is that when I plug the antenna out completely and run the program FM transmits correctly (though with less power because of the lack of an antenna), but this time the pogram runs (semi) correctly. The FM is correctly transmited and the LCD is working with sudden minor birghtness changes. After a minute or so the Pi crashes and shuts down completely. Could this be a power supply issue? Or maybe the strength of the FM signal interferes with Pi's electronics? Ugh...
    – Ciprum
    Aug 13, 2016 at 13:00
  • I suppose it could be a power supply issue. I'd be surprised if you could do anything just by modulating a signal from a pin that would screw up the functioning of the other pins, although I believe antennas have capacitance and thus may have an odd effect on the pin (? not sure, you could ask on Electrical Engineering).
    – goldilocks
    Aug 13, 2016 at 13:08
  • I did find here an implication that the methodology behind PiFM may skew the clock down -- although I could be reading too much into that and have not seen or found people complaining of this (but I did not search hard). BTW, there are dedicated FM transmitter devices you can get which can be hooked up to the pi for ~$20 USD.
    – goldilocks
    Aug 13, 2016 at 13:14
  • That's interesting. I'll try using a photoresistor and a RTC via I2C and see if they will work while transmiting FM. Anyways do you know a method of validating if I2C data is corrupt?
    – Ciprum
    Aug 13, 2016 at 16:41
  • After my little test I realised that just simply running Pi2Fm completely freezes my Pi and it even eventually crashes. I will check resource usage while transmiting FM to make sure this isn't eating up all of the CPU and other resources.
    – Ciprum
    Aug 13, 2016 at 16:54
0

You can also create aditional i2c busses on the raspberry pi to solve i2c conflicts

https://www.instructables.com/id/Raspberry-PI-Multiple-I2c-Devices/

1
  • 2
    Hi. Answers are supposed to contain the basic information without clicking a link. (A link as a supplement is fine.) Can you expand this answer? Otherwise it risks getting closed.
    – Brick
    Dec 24, 2018 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.