Minimal Code (python) using Rasberry Pico

An area for more general discussion by the user community for topics not covered elsewhere
Post Reply
marcvivet
Posts: 7
Joined: Fri Sep 02, 2022 4:17 pm

Minimal Code (python) using Rasberry Pico

Post by marcvivet »

Hi,

I started a new project using the Raspberry pi Pico and I wanted to create a simple remote IR controller for my air conditioner.

Using the USB analysIR I have extracted the Start "code" of my air conditioner and I wrote this code:

Code: Select all

from machine import Pin
import utime
import time

# Extracted using AnalysIR RAW format
data = '3358 -1678 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -1260 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 404 -11364 3358 -1678 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -1260 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420'

iro = Pin(17, Pin.OUT, Pin.PULL_DOWN)

code = [int(element) for element in data.split(' ')]

def send_code(pin, code):
    for element in code:
        [pin.on, pin.off][element < 0]()
        utime.sleep_us(abs(element))

while True:
    send_code(iro, code)
    print('ok')
    iro.off()
    time.sleep(3)
    
However it is not working, I have connected the IR led directly to the Pin 17 of my rasberry pi Pico. I tried with the leds provided in the kid "Infrared DIY Soldering Kit".

I have also tried to connect the SendIR module but without success. It flashes the led following a pattern but the air conditioner do not turns on. Using the USB Stick and the program LearnIR it works fine.

If anyone could help me with this I would be very grateful ... Thanks in advance!
User avatar
AnalysIR
Site Admin
Posts: 776
Joined: Sat Aug 31, 2013 3:51 pm
Location: Dublin, Ireland
Contact:

Re: Minimal Code (python) using Rasberry Pico

Post by AnalysIR »

Unfortunately, we don't support the RPI PIco at this time and would try to avoid using python (vs c).

The good news is that I imported your signal timings into AnalysIR and it decodes as Mitsubishi 288 AC signal.

Based on a quick read of your code you are not providing any pwm to the carrier on the marks, which would be a fatal problem.
marcvivet
Posts: 7
Joined: Fri Sep 02, 2022 4:17 pm

Re: Minimal Code (python) using Rasberry Pico

Post by marcvivet »

Thanks for the quick response, do you have a similar code but in C? So I can have a reference ...
marcvivet
Posts: 7
Joined: Fri Sep 02, 2022 4:17 pm

Re: Minimal Code (python) using Rasberry Pico

Post by marcvivet »

I saw some but I do not understand how to translate the AnalysIR output to pwm duty values ...

I have to set the frequency to 38khzs but then to convert the hexadecimal values to duty values for each pair is not clear ...
User avatar
AnalysIR
Site Admin
Posts: 776
Joined: Sat Aug 31, 2013 3:51 pm
Location: Dublin, Ireland
Contact:

Re: Minimal Code (python) using Rasberry Pico

Post by AnalysIR »

Ir signals are made up of marks & spaces. In your signal the negative values are spaces. Tje non-negative are marks.

A space is essentially no IR is sent.

A mark is sent as a modulated(PWM) signal. see https://www.sbprojects.net/knowledge/ir/index.php
There are many pages of info under the menu.


try this library: https://github.com/Arduino-IRremote/Arduino-IRremote
marcvivet
Posts: 7
Joined: Fri Sep 02, 2022 4:17 pm

Re: Minimal Code (python) using Rasberry Pico

Post by marcvivet »

I finally managed to make it work in a raspberry pi pico using micro Python:

Here is the code:

Code: Select all

from machine import Pin, PWM
import utime
import time

iro = Pin(17, Pin.OUT, Pin.PULL_DOWN)
iro.off()
    
def send_code(pin, code):
    pwm = PWM(pin)
    pwm.freq(38000)

    for element in code:
        if element > 0:           
            pwm.duty_u16((2**16)//3)
        else:
            pwm.duty_u16(0)
            
        utime.sleep_us(abs(element))
               
    pwm.deinit()
    pin.off()
    
    
data = '3358 -1678 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -1260 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 404 -11364 3358 -1678 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -1260 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -420 420 -1260 420 -420 420 -420 420 -1260 420 -1260 420 -420 420 -1260 420'

code = [int(element) for element in data.split(' ')]

while True:
    send_code(iro, code)
    print('ok')
    time.sleep(1)
    
By changing the "data" variable you can call any IR function.

I was able to do this thanks to the LearnIR to debug the signals, amazing tool :). I connected the Led directly to the Pin 22 (positive) and Pin 23 (negative) and works fine (you need to be close to the device ...).
Post Reply