Raspberry Pi Pico - problem with Thonny interpreter - raspberry-pi

I have a trouble with programming Raspberry Pi Pico. I am using Thonny IDE and micropython. I am just a beginner so just download code from their website (https://projects.raspberrypi.org/en/projects/getting-started-with-the-pico/6) and install it to microcontroller. But when I save this code:
from machine import Pin
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
if button.value():
led.toggle()
time.sleep(0.5)
I receive this message:
Traceback (most recent call last):
File "", line 10
IndentationError: unindent doesn't match any outer indent level
Could you help me please?

You need to indent your code properly:
from machine import Pin
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
if button.value():
led.toggle()
time.sleep(0.5)

Maybe try this:
from machine import Pin
import time
led = Pin(15, Pin.OUT)
button = Pin(14, Pin.IN, Pin.PULL_DOWN)
while True:
if button.value():
led.toggle()
time.sleep(0.5)
I just changed if button.value(): four more spaces in so its inside the while true: loop.

Related

Raspberry Pico W network test fails

I just put my hands on RP Pico W to try it out but I hit the issue with the very first wifi test.
Tutorials say to do this:
import network
import socket
from time import sleep
from picozero import pico_temp_sensor, pico_led
import machine
so my pico lib folder looks like this when I imported all required packages:
However when try to run wifi test like this:
def connect():
#Connect to WLAN
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
ip = wlan.ifconfig()[0]
print(f'Connected on {ip}')
try:
connect()
except KeyboardInterrupt:
machine.reset()
it fails with following error:
>>> %Run -c $EDITOR_CONTENT
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/lib/network.py", line 8, in <module>
File "/lib/socket.py", line 42, in <module>
AttributeError: 'module' object has no attribute 'socket'
>>>
This is whole code, I skipped only ssid and password.
I cannot figure out what exactly is wrong here. I do not have any other files named the same as socket or so.
I took the example from RP foundation pages:
https://projects.raspberrypi.org/en/projects/get-started-pico-w/2

Micropython: [Errno 19] ENODEV but i2c.scan() returns proper address

I am currently using an SRF-10 sensor connected to an esp32. It's being powered by 5V and I am using a level converter to bring down the voltage to 3.3V to be able to use it on the esp32. Both the SCL and SDA have a 1.8K pull up resistor as recommended on the datasheet.
I have written the following script to try to get a read from the sensor. I am not entirely sure if it is correct but soon as it reaches line 16 I get an error saying [Errno 19] ENODEV. Eveything I could find suggests that the i2c connection isn't working properly but when I run i2c.scan() it returns the sensor address so I am guessing connections arenĀ“t the problem.
My script is as follows:
from machine import I2C, Pin
import time
byte = bytearray(4)
#Distance units
unit_in = 0x50
unit_cm = 0x51
unit_us = 0x52
i2c = I2C(scl=Pin(21), sda=Pin(22))
address = i2c.scan()[0]
print(address)
#Sensor range
range_mm = 11008 // 43 - 1
i2c.writeto_mem(range_mm, address, bytearray(2)) #line 16
#Begin reading
i2c.writeto_mem(unit_cm, address, bytearray(0))
#Reading after measurement
data = i2c.readfrom_mem(4, address, 0)
print(data)
This is the output:
112
Traceback (most recent call last):
File "main.py", line 22, in <module>
OSError: [Errno 19] ENODEV
MicroPython v1.12 on 2019-12-20; ESP32 module with ESP32
Type "help()" for more information.
What could I be doing wrong?
The proper use of i2c.writeto_mem requires the following order of arguments:
writeto_mem(addr, memaddr, buf, *, addrsize=8)
Therefore, try to use in line 16:
i2c.writeto_mem(address, range_mm, bytearray(2)) #line 16
Reference: class I2C

Raspberry PI 4 RPi.GPIO library doesn't work on Ubuntu 20.10

I'm using Ubuntu 20.10 on Raspberry Pi 4 4GB, but using RPi.GPIO library on my code Terminal shows to me this message:
"GPIO.setup(INT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
RuntimeError: Not running on a RPi!"
This is the code:
#!/usr/bin/env python2.7
import RPi.GPIO as GPIO
import subprocess
from time import sleep
GPIO.setmode(GPIO.BCM) # use GPIO numbering
GPIO.setwarnings(False)
print('RPi.GPIO:',GPIO.VERSION)
INT = 12 # GPIO-12 button interrupt to shutdown procedure
# use a weak pull_up to create a high
GPIO.setup(INT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def main():
while True:
# set an interrupt on a falling edge and wait for it to happen
GPIO.wait_for_edge(INT, GPIO.FALLING)
# we got here because the button was pressed.
# wait for 3 seconds to see if this was deliberate
sleep(3)
# check the button level again
if GPIO.input(INT) == 0:
# still pressed, it must be a serious request; shutdown Pi
subprocess.call(['poweroff'], shell=True, \
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if __name__ == '__main__':
main()
I can't found the mistake of the code, Is there any problem between Ubuntu and this library?
Thank you so much!

Google Coral USB Accelerator on Raspbian Lite Buster and Raspberry PI 4

I am following the getting started guide for the Google Coral USB Accelerator and even though I was able to trick the install.sh file to install for RPI4
https://twitter.com/hansamann/status/1154021771554766849
I cannot run the example with the image classification. When I run the sample, I get this:
python3 classify_image.py --model ~/Downloads/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite --label ~/Downloads/inat_bird_labels.txt --image ~/Downloads/parrot.jpg
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/edgetpu/swig/edgetpu_cpp_wrapper.py", line 18, in swig_import_helper
fp, pathname, description = imp.find_module('_edgetpu_cpp_wrapper', [dirname(__file__)])
File "/usr/lib/python3.7/imp.py", line 296, in find_module
raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_edgetpu_cpp_wrapper'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "classify_image.py", line 19, in <module>
from edgetpu.classification.engine import ClassificationEngine
File "/usr/local/lib/python3.7/dist-packages/edgetpu/classification/engine.py", line 17, in <module>
from edgetpu.basic.basic_engine import BasicEngine
File "/usr/local/lib/python3.7/dist-packages/edgetpu/basic/basic_engine.py", line 15, in <module>
from edgetpu.swig.edgetpu_cpp_wrapper import BasicEngine
File "/usr/local/lib/python3.7/dist-packages/edgetpu/swig/edgetpu_cpp_wrapper.py", line 28, in <module>
_edgetpu_cpp_wrapper = swig_import_helper()
File "/usr/local/lib/python3.7/dist-packages/edgetpu/swig/edgetpu_cpp_wrapper.py", line 20, in swig_import_helper
import _edgetpu_cpp_wrapper
ModuleNotFoundError: No module named '_edgetpu_cpp_wrapper'
Any idea whay I could try?
I had the same problem but with an older Rpi 3. This is the solution I used. I don't think it's a make or break to complete the project:
$ sudo cp _edgetpu_cpp_wrapper.cpython-35m-arm-linux-gnueabihf.so _edgetpu_cpp_wrapper.cpython-37m-arm-linux-gnueabihf.so
Can you go through : https://github.com/google-coral/edgetpu-platforms ?
This repository holds auxiliary platform-related material related to Google Coral Edge TPU. Here you can find precompiled images, shared libraries and patches for using the USB Edge TPU accelerator on additional platforms to the main supported ones.
For convenience Google has uploaded prebuilt images for Raspberry Pi Zero, Pi 3 and Pi 4. Simply write the image to an sd card and boot up your Pi. The images contain several examples that should work out of the box.

Ipython notebook: in computing SIFT freatures using Python-Opencv: The kernel appears to have died/ in Python:Segmentation fault (core dumped)

Hi trying to execute the following simple program to detect SIFT features of an image in Ipython notebook, but everytime receiving an error message appears. I googled it, but couldn't find any solutions so far.
import cv2
import numpy as np
import itertools
Load Images:
img = cv2.imread("galaxy.png")
Find Keypoints:
detector = cv2.FeatureDetector_create("SIFT")
descriptor = cv2.DescriptorExtractor_create("SIFT")
skp = detector.detect(img) # in this line kernel dies!!
the kernel appears to have died
skp, sd = descriptor.compute(img, skp)
tkp = detector.detect(template)
tkp, td = descriptor.compute(template, tkp)
my system is Ubuntu 14.04 lts, ipython 3.2.0, python 2.7.
more over I tried it in python and got this error message:
Segmentation fault (core dumped)
I found a way around, but not a good way. I moved that notebook to another folder which it was the only file in that folder, ran it again and got no error. the bad side is that I need this notebook to be in a specific folder! Really Strange problem. any idea how to solve this problem in a better way!