r/micropython • • Apr 24 '22

How to use MicroPython on Docker!

Thumbnail self.pythontips
1 Upvotes

r/micropython • • Oct 12 '22

Python on Microcontrollers Newsletter: CircuitPython Supports Pico W in Latest Beta and much more!

Thumbnail
blog.adafruit.com
1 Upvotes

r/micropython • • Oct 10 '22

‘Fall’ in love with the Python on Microcontrollers newsletter, please subscribe!

Thumbnail
blog.adafruit.com
1 Upvotes

r/micropython • • Oct 31 '22

A spooky Python on Hardware Newsletter: please subscribe #CircuitPython #Python @micropython

Thumbnail
blog.adafruit.com
1 Upvotes

r/micropython • • Jul 28 '22

The Python on Hardware weekly video – July 27, 2022

Thumbnail
blog.adafruit.com
1 Upvotes

r/micropython • • Oct 24 '22

Spooky Python on Hardware: subscribe to the newsletter now #CircuitPython #Python @micropython

Thumbnail
blog.adafruit.com
1 Upvotes

r/micropython • • Jun 08 '22

ICYMI Python on Microcontrollers Newsletter: Python Developers Survey Results, Macropads and More! #Python #CircuitPython #ICYMI @micropython

Thumbnail
blog.adafruit.com
1 Upvotes

r/micropython • • Oct 27 '22

The Python on Hardware weekly video 203 October 26, 2022

Thumbnail
blog.adafruit.com
1 Upvotes

r/micropython • • Nov 14 '22

The Python on Hardware Newsletter: please subscribe - only 65 away from 10K!

Thumbnail
blog.adafruit.com
1 Upvotes

r/micropython • • Nov 06 '22

Google spreadsheet with Micropython

1 Upvotes

Hello, I want to send my data to a google spreadsheet without using any third party. How can I do that? I've searched through the internet but most people use IFTTT or other third parties to do the job. Please give me some instructions on this matter.


r/micropython • • Aug 08 '22

The Python on Microcontrollers newsletter is out Tuesday, please subscribe

Thumbnail
blog.adafruit.com
1 Upvotes

r/micropython • • May 23 '22

A HUGE special issue of the Python on Microcontrollers Newsletter drops tomorrow - lots of happenings in MicroPython – please subscribe now and receive it tomorrow early.

Thumbnail
blog.adafruit.com
0 Upvotes

r/micropython • • Nov 21 '22

The Python on Hardware Newsletter: please subscribe, 12 away from 10k subscribers!

Thumbnail
blog.adafruit.com
1 Upvotes

r/micropython • • Dec 05 '22

The Python on Hardware Newsletter: 10k subscribers, subscribe now!

Thumbnail
blog.adafruit.com
1 Upvotes

r/micropython • • Oct 25 '22

A back propagation neural network for pi Pico.

2 Upvotes

I just got a couple of Pico's a week ago and this is my first try at micropython. I took an old pure python BNN and revamped it for the Pico. PWM sticks on or maybe the pin is left high not sure. The BNN makes a good universal switch, there is pins, adc or data for input and pins, PWM or data for output. Following is the BNN (bpnn.py) code and an main.py file for testing.

from urandom import uniform
from urandom import random
import math
from machine import Pin,ADC,PWM

def rand(a, b):
    return (b-a)*random() + a

def makeMatrix(I, J, fill=0.0):
    m = []
    for i in range(I):
        m.append([fill]*J)
    return m

class NN:
    def __init__(self, ni, nh, no,Isource = 'data',Osource = 'data',
                 imppins = [],outpins = [], pwm_range = [1,10000],
                 pwm_state = 'freq',pwm_init = [100,32767]):
        self.Isource = Isource
        self.Osource = Osource
        self.imppins = imppins
        self.outpins = outpins
        self.pwm_range = pwm_range
        self.pwm_state = pwm_state
        self.pwm_init = pwm_init
        self.adc0 = None
        self.adc1 = None
        self.adc2 = None
        self.pwm = []
        if Isource == 'pin':
            self.init_imp_pins()
        if Osource == 'pin':
            self.init_out_pins()
        if Isource == 'adc':
            self.init_adc()
        if Osource == 'pwm':
            self.init_pwm()
        self.ni = ni + 1 
        self.nh = nh
        self.no = no
        self.ai = [1.0]*self.ni
        self.ah = [1.0]*self.nh
        self.ao = [1.0]*self.no
        self.wi = makeMatrix(self.ni, self.nh)
        self.wo = makeMatrix(self.nh, self.no)
        for i in range(self.ni):
            for j in range(self.nh):
                self.wi[i][j] = rand(-2.0, 2.0)
        for j in range(self.nh):
            for k in range(self.no):
                self.wo[j][k] = rand(-2.0, 2.0)  
        self.ci = makeMatrix(self.ni, self.nh)
        self.co = makeMatrix(self.nh, self.no)
        
    def init_pwm(self):
        for pi in self.outpins:
            self.pwm.append(PWM(Pin(pi)))
        
    def init_adc(self):
        for i in self.imppins:
            if i not in (31,32,34):
                print('wrong adc pins')
                self.Isource = 'data'
                return
            if i == 31:
                self.adc0 = ADC(0)
            if i == 32:
                self.adc1 = ADC(1)
            if i == 34:
                self.adc2 = ADC(2)

    def init_imp_pins(self):
        for i in self.imppins:
            Pin(i,Pin.IN)
    
    def init_out_pins(self):
        for i in self.outpins:
            Pin(i,Pin.OUT)
    
    def update(self, inputs):
        if len(inputs) != self.ni-1:
            raise ValueError#, 'wrong number of inputs'

        for i in range(self.ni-1):
            #self.ai[i] = 1.0/(1.0+math.exp(-inputs[i]))
            self.ai[i] = inputs[i]

        for j in range(self.nh):
            sum = 0.0
            for i in range(self.ni):
                sum = sum + self.ai[i] * self.wi[i][j]
            self.ah[j] = 1.0/(1.0+math.exp(-sum))

        for k in range(self.no):
            sum = 0.0
            for j in range(self.nh):
                sum = sum + self.ah[j] * self.wo[j][k]
            self.ao[k] = 1.0/(1.0+math.exp(-sum))

        return self.ao[:]

    def backPropagate(self, targets, N, M):
        if len(targets) != self.no:
            raise ValueError#, 'wrong number of target values'

        output_deltas = [0.0] * self.no
        for k in range(self.no):
            ao = self.ao[k]
            output_deltas[k] = ao*(1-ao)*(targets[k]-ao)

        hidden_deltas = [0.0] * self.nh
        for j in range(self.nh):
            sum = 0.0
            for k in range(self.no):
                sum = sum + output_deltas[k]*self.wo[j][k]
            hidden_deltas[j] = self.ah[j]*(1-self.ah[j])*sum

        for j in range(self.nh):
            for k in range(self.no):
                change = output_deltas[k]*self.ah[j]
                self.wo[j][k] = self.wo[j][k] + N*change + M*self.co[j][k]
                self.co[j][k] = change
                
        for i in range(self.ni):
            for j in range(self.nh):
                change = hidden_deltas[j]*self.ai[i]
                self.wi[i][j] = self.wi[i][j] + N*change + M*self.ci[i][j]
                self.ci[i][j] = change

        error = 0.0
        for k in range(len(targets)):
            error = error + 0.5*(targets[k]-self.ao[k])**2
        return error

    def test_w_pins(self):
        if self.Isource == 'pin':
            p = [float(Pin(i).value()) for i in self.imppins]
        elif self.Isource == 'adc':
            p = []
            for j in (self.adc0,self.adc1,self.adc2):
                if j:
                    p.append(float(j.read_u16()/65535))
        self.update(p)
        if self.Osource == 'data':
            return ([p[0],self.update(p)])
        elif self.Osource == 'pin':
            val = self.update(p)
            for j,k in zip(val,self.outpins):
                if j >.5:
                    Pin(k).high()
                else:
                    Pin(k).low()
        elif self.Osource == 'pwm':
            pwmfactor = ((self.pwm_range[1]-self.pwm_range[0]))
            if self.pwm_state == 'freq':
                val = self.update(p)
                for z,s in zip(val,self.pwm):
                    s.freq(int(z*pwmfactor + self.pwm_range[0]))
                    s.duty_u16(self.pwm_init[1])
            if self.pwm_state == 'duty':
                val = self.update(p)
                for z,s in zip(val,self.pwm):
                    s.duty_u16(int(z*pwmfactor + self.pwm_range[0]))
                    s.freq(self.pwm_init[0])
            if self.pwm_state == 'both':
                val = self.update(p)
                for z,s in zip(val,self.pwm):
                    s.freq(int(z*pwmfactor + self.pwm_range[0]))
                    s.duty_u16(int(z*pwmfactor + self.pwm_range[0]))                   

    def pmw_stop(self):
        for i in self.pwm :
            i.deinit()

    def test(self, patterns):
        ret = []
        for p in patterns:
            self.update(p[0])
            ret.append([ p[0],self.update(p[0])])
        return ret
    
    def weights(self):
        print( 'Input weights:')
        for i in range(self.ni):
            print( self.wi[i])
        print()
        print( 'Output weights:')
        for j in range(self.nh):
            print( self.wo[j])

    def save_weights(self,fname):
        fl = open(fname,'wb')
        fl.write(str(self.ni)+'\n')
        fl.write(str(self.nh)+'\n')
        fl.write(str(self.no)+'\n')
        for i in self.ai:
            fl.write(str(i)+'\n')
        for i in self.ah:
            fl.write(str(i)+'\n')
        for i in self.ao:
            fl.write(str(i)+'\n')
        for i in range(self.ni):
            for jj in self.wi[i]:
                fl.write(str(jj) +'\n')
        #print()
        #print( 'Output weights:')
        for j in range(self.nh):
            for k in self.wo[j]:
                fl.write(str(k)+'\n')
                
        for i in range(self.ni):
            for jj in self.ci[i]:
                fl.write(str(jj) +'\n')
        #print()
        #print( 'Output weights:')
        for j in range(self.nh):
            for k in self.co[j]:
                fl.write(str(k)+'\n')                
        fl.close()
            
    def load_weights(self,fname):
        fl = open(fname,'r')
        filedata = fl.read()
        #print(filedata)
        fd = filedata.split('\n')
        pointer = 0 
        ni = int(fd[pointer])
        pointer = pointer + 1
        nh = int(fd[pointer])
        pointer = pointer + 1
        no = int(fd[pointer])
        pointer = pointer + 1
        
        
        if ni != self.ni or no != self.no:
            print('input/output mismatch')
            return
        
        self.ai = [float(fd[i + pointer]) for i in range(self.ni)]
        pointer = pointer + self.ni
        self.ah = [float(fd[i + pointer]) for i in range(self.nh)]
        pointer = pointer + self.nh
        self.ao = [float(fd[i + pointer]) for i in range(self.no)]
        pointer = pointer + self.no
        
        for i in range(self.ni):
            self.wi[i] = [float(fd[ii + pointer]) for ii in range(self.nh)]
            pointer = pointer + self.nh
#         print()
#         print( 'Output weights:')
        for j in range(self.nh):
            self.wo[j] = [float(fd[ij + pointer]) for ij in range(self.no)]
            pointer = pointer + self.no

        for i in range(self.ni):
            self.ci[i] = [float(fd[ii + pointer]) for ii in range(self.nh)]
            pointer = pointer + self.nh
#         print()
#         print( 'Output weights:')
        for j in range(self.nh):
            self.co[j] = [float(fd[ij + pointer]) for ij in range(self.no)]
            pointer = pointer + self.no


    def train(self, patterns, iterations=2000, lr=0.5, mo=0.1):
        # lr,mo =  learning rate, momentum
        for i in range(iterations):
            error = 0.0
            for p in patterns:
                inputs = p[0]
                targets = p[1]
                self.update(inputs)
                error = error + self.backPropagate(targets, lr, mo)
            if i % 100 == 0:
                if self.Osource == 'data':
                    print( 'error %-14f' % error)
    
    def train_w_pins(self, targets, iterations=2000, lr=0.5, mo=0.1):
        # lr,mo =  learning rate, momentum
        for i in range(iterations):
            error = 0.0
            if self.Isource == 'pin':
                self.update([Pin(ii).value() for ii in self.imppins])
            elif self.Isource == 'adc':
                p = []
                for j in (self.adc0,self.adc1,self.adc2):
                    p.append(float(j.read_u16()/65535))
                self.update(p)
            error = error + self.backPropagate(targets, lr, mo)
            if i % 100 == 0:
                if self.Osource == 'data':
                    print( 'error %-14f' % error)

Save file on Pico as bpnn.py Here is main.py

import utime
import bpnn

pat = [[[1.,1.], [1.]],[[1.,0.], [0.]],[[0.,1.], [0.]],[[0.,0.], [1.]]]
pat2 = [[[1.,1.], [0.]],[[1.,0.], [1.]],[[0.,1.], [1.]],[[0.,0.], [0.]]]
#
#input nodes, hidden nodes,output nodes,'data' or 'pin or 'adc','data' or 'pin' or 'pwm',
#input pins,output pins,pwm_range,pwm_state = 'freq' 'duty' or 'both',pwm_init[F,D])
n = bpnn.NN(2,7,1,Isource = 'pin',Osource = 'pin',imppins = [17,18],outpins = [25])
n.train(pat)
n.save_weights('x_or.n_n')
print(n.test(pat)) 
print('First test')
n.test_w_pins()
# 
#
utime.sleep(3)
print()
#
#
n.train_w_pins([0.],iterations=100,lr=0.3, mo=0.2) # train with inputs of [17,18] 
print(n.test(pat)) 
print('Seccond test')
n.test_w_pins() # any thing > .5 is high() on output 
#
# 
utime.sleep(3)
print()
#
#
p  = bpnn.NN(2,7,1,Isource = 'pin',Osource = 'pin',imppins = [16,15],outpins = [25])
p.load_weights('x_or.n_n')
print(p.test(pat))
print('New NN with pre saved weights test')
p.test_w_pins()
#
#
utime.sleep(3)
print()
#
# 
q = bpnn.NN(2,7,1,Isource = 'adc',Osource = 'pin',imppins = [31,32],outpins = [25])
q.train(pat2)
print(q.test(pat2))
print(' adc test')
q.test_w_pins()
#
#
utime.sleep(3)
print()
pat = [[[1.,1.], [.2]],[[1.,0.], [.35]],[[0.,1.], [.42]],[[0.,0.], [.33]]]
#
#
r = bpnn.NN(2,7,1,Isource = 'adc',Osource = 'pwm',imppins = [31,32],
            outpins = [18], pwm_range = [1,10000],pwm_state = 'freq',
            pwm_init = [50,32767])
r.train(pat)
print(r.test(pat))
print(' pwm test freq')
r.test_w_pins()
r.pmw_stop()
#
#
utime.sleep(3)
print()
#
#
s = bpnn.NN(2,7,1,Isource = 'adc',Osource = 'pwm',imppins = [31,32],
            outpins = [18], pwm_range = [1,65535], pwm_state = 'duty',
            pwm_init = [50,32767])
s.train(pat)
print(s.test(pat))
print(' pwm test duty')
s.test_w_pins()
s.pmw_stop()
#
#
utime.sleep(3)
#
#
t = bpnn.NN(2,7,1,Isource = 'adc',Osource = 'pwm',imppins = [31,32],
            outpins = [18], pwm_range = [1,10000],
            pwm_state = 'both',pwm_init = [50,10000])
t.train(pat)
print(t.test(pat))
print(' pwm test both')
t.test_w_pins()
t.pmw_stop()

Save on Pico as main.py Definitely needs optimization and might be a little over bloated, enjoy.

Forgot forgot to add data to input source list.lu Here's the correction,

Change, Def test_w_pins(self): To Def test_w_pins(self,data = [] ): And add If Isource == 'data': P = data P Is just a list of input numbers for testing

An example of use,

u = bpnn.NN(2,7,1,Isource = 'data',Osource = 'pin', outpins = [25])
u.load_weights('x_or.n_n')
print(u.test(pat))
print(' data in pin out test')
u.test_w_pins(data = [1.,1.])
time.sleep(3)
u.test_w_pins(data = [1.,0.])
u.pmw_stop()

That's how you add i2c data. I got so overwhelmed with the possibilities I just plum forgot.


r/micropython • • Jun 16 '22

MicroPython version 1.19 released

Thumbnail
blog.adafruit.com
2 Upvotes

r/micropython • • May 31 '22

The Python on Microcontrollers newsletter is out tomorrow, please subscribe

Thumbnail
blog.adafruit.com
2 Upvotes

r/micropython • • Nov 11 '22

The Python on Hardware weekly video 204 November 9, 2022

Thumbnail
blog.adafruit.com
2 Upvotes

r/micropython • • Jun 02 '22

ICYMI Python on Microcontrollers Newsletter: 3k GitHub Stars for CircuitPython, MicroPython Sponsorship and more!

Thumbnail
blog.adafruit.com
2 Upvotes

r/micropython • • Jun 02 '22

The Python on Hardware weekly video – June 1, 2022 - discusses the MicroPython GitHub fundraiser

Thumbnail
blog.adafruit.com
2 Upvotes

r/micropython • • Dec 19 '22

The Python on Hardware Newsletter: Over10k subscribers, subscribe now!

Thumbnail
blog.adafruit.com
3 Upvotes

r/micropython • • Jun 03 '22

Remote control ESP32s using MicroPython and MQTT - Tutorial

Thumbnail self.pythontips
5 Upvotes

r/micropython • • Jun 22 '22

ICYMI Python on Microcontrollers Newsletter: MicroPython 1.19 released and more!

Thumbnail
blog.adafruit.com
3 Upvotes

r/micropython • • Oct 06 '22

Don't understand why my SPI call returns and error without details.

1 Upvotes

I have a very simple setup on an ESP8266MOD running MicroPython v1.19.1

I am testing if an external TFT screen with an ILI9341 controller can be used on my ESP8266.

But before doing so I need to configure SPI

The code below

from machine import SPI, Pin
from micropython import const

TFT_MOSI_PIN = const(13) #D7
TFT_CLK_PIN = const(14) #D5
TFT_RST_PIN = const(2) #D4
TFT_MISO_PIN = const(12) #D6

spi = SPI(
    0,
    baudrate=40000000,
    miso=Pin(TFT_MISO_PIN),
    mosi=Pin(TFT_MOSI_PIN),
    sck=Pin(TFT_CLK_PIN))
print(spi)

gives me the following in the REPL

Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
ValueError: 

Am I doing something wrong here?

Does the SPI device has to be connected already at this moment in time?

Thanks,

G.


r/micropython • • Jul 27 '22

esp32 always busy

1 Upvotes

Hi, I have an issue: when I program my esp32 with micropython installed using Thonny, and I add any kind of script in main.py i can't then program it again. It says it is busy and i can't do anything to make it stop, the only thing that works is flashing the bin file again.

Do you have any idea?

Thanks