How to program a atMega328p MCU with a Raspberry Pi 4 (Python) - python

I been looking everywhere online for this exact configuration but can't find much.
I want to program my AtMega328p MCU (its on a breadboard) using Python from my Raspberry Pi 4 but I am not sure how to check if communication is going on between them? I have the MISO, MOSI, SCLK, and CE0 pins from the Pi connected to the MISO, MOSI, SCK, SS pins on the AtMega328p respectively.
I understand I have to use SPI communication, however how can I exactly send data from the Raspberry Pi to the MCU to ensure there is communication between the two? Maybe some code to send to the MCU and receive it back? I been using the SPI Dev Python libraries but can't find much info on it. Thank you in advance!!

To load code onto the AVR (program it), you want to use existing software like avrdude that already speaks the AVR ISP protocol. avrdude already has support for using the RPi SPI headers - just use the linuxspi programmer type.
Here's an article by the author that explains it (although there are probably more recent articles if you search around) http://kevincuzner.com/2013/05/27/raspberry-pi-as-an-avr-programmer/
Is there a reason you want to do this from Python specifically? Or are you referring to communicating between the uC firmware and some Python code on the Pi?

Related

Accessing Raspberry Pi4 USB port with Python

I am using Raspberry Pi 4 which comes with 4 USB ports. For my project, I have two mics and both will be connected to Rsp via a USB port. I need to be able to choose which Mic needs to be ON. So basically I need to be able to switch/choose which USB port I want the sound to come in.
I need to be able to choose Mic1 or Mic2. I am writing my code in Python. Is there any library that gives me access to choose a specific UBS port? I really can't find anything.
Appreciate your help.
Regards

Sending data from PC to Raspberry Pi using Python

I am working on a project where I need to detect objects from IP camera on PC using TensorFlow and the data of the detected object to raspberry pi and control its GPIO pins accordingly.
I am using python on both ends.
I read somewhere to form a client-server network between the two.
I need some ideas on how to approach this problem. Thank you. I hope my question was clear.
Extra Information: I am currently using Raspberry pi on PC via ethernet cable. Also, both raspberry pi and my PC are connected to the same Wifi router. I hope this information helps.
You can use sockets to make the Raspberry Pi and your PC communicate. You can use socket.accept(conn, address) to accept a connection and conn.recv(buffer) to receive data.
I'm a big fan of using Redis for this type of thing. It has a small footprint, is simple to set up and is lightning fast. It is a "networked data structure server". "Networked" means that its clients can be on the same machine or anywhere on the network. The data structures it serves include:
strings
integers
atomic integers
lists
queues
sets
sorted sets
hashes
There are bindings for Python, C, C++, PHP, Ruby and rather brilliantly, the shell. That means you can inject values into queues, lists or sets directly from any Terminal on your network whilst your app is running, in order to test it or monitor it.
Example here.

send 2Dimensional Listarray From Raspberry pi to the Arduino with I2c protocol

im working on Computer vision (opencv )python and i had a result from the image , so this results is 2D List-arrays that should go to the Arduino by i2c buffer , so i realized that there is a library called smbus that interfacing the Raspberry pi with i2c ports so send and receive data , So i searched on References pages that give me some explanation about this Library but i didn't found eny thing useful...and all what i found is this sites which is not enough information
http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/plain/Documentation/i2c/smbus-protocol
http://wiki.erazor-zone.de/wiki:linux:python:smbus:doc
so im indeed need eny explanation how to send 2D Arrays Like (x,y) Coordinates
from Pi to Arduino with i2c buffer
thanks in advance
Check this repository. We connected a NTC CHIP to a Adafruit Trinket using I2C, but connecting the PI with the an Arduino should be very much the same.
The key file is the I2C.py file, which uses SMBus.
To talk to the Trinket Pro (ATMega328) we used the trinket.py file.
And the corresponding client code in the ATMega328, using the Arduino library, specially the Wire.h is located in the hwthontrinket.ino file.
Finally, you can check how to use the classes in the test files. You basically need to pass the bus number and the address of the device.

Reading Analog Data from sensor connected to USB (Python)

I may be confusing a few concepts here so any help is appreciated.
Q1: Is it possible to attach any sensor in the world to the USB on my computer as long as it gives me analog data, and read its output? (e.g. pH, temperature, oxygen sensor etc as long as it gives me analog data)
Q2: If so, then what is the simplest way in python for me read such data.
Comment: I am trying to bypass using PLC's, and trying to see if I can get the output from the sensor directly to the PC. (I do not have drivers for these sensors)
Actual Need: I have an oxygen sensor connected to my computer via a USB. The oxygen sensor is able to send out analog data. The obvious way is to go through a PLC. However, I would like a solution which by-passes PLC's so I can connect the sensor directly to my PC via USB.
The USB port on your computer cannot read analog data because USBs work with digital signals. You would need an analog-to-digital converter (ADC).
as #digitaLink answered, it is not possible directly via USB and yes, the obvious way is to use a PLC.
I would go the PLC way - in fact, I did it a few times in the past - and start with an Arduino and later develop a custom PCB, put it in a box and done.
Another possibility is to use a raspberry pi (or similar SBC), which has the GPIOs you can use for analog read.
Edit: there is another possibility.
The sensor you use now is _very_likely_ a PLC in itself, that is, the sensor is attached to a microcontroller that uses the USB port for serial communication. Now, the drivers you are missing do nothing else but decode the data coming through the serial port. Take a look inside your harware and try to find out what components there are in.
So what you could do is to try to find out how to communicate with the sensor via a serial terminal. It is probably possible to monitor serial communication (although I must admit, I don't know how to do that), reverse engineer the code and write your own driver in python. You could learn a lot from this, even if you don't succeed.

Writing web app to program Arduino

I want to write a web app allowing people to program Arduino remotely. There will be a live video stream of the Arduino with lolShield.
What's the best way to flash the Arduino within Python?
inotool is a commandline wrapper for the sketch environment and is written in python.
Or you could use exec() to run one of the commandline solutions in Command line Arduino compiling and downloading?
avrdude is the tool to program an Arduino over a serial interface. So basically you need a serial interface from your web server to the Arduino in question. Here are two approaches that may help you get there (I had tried the first one about 1.5 years ago, it worked like a breeze):
Xbee RF modules can transparently be operated in serial mode, perfect for this use case. The downside of this is that you need a local in-between machine that is connected to the internet and acts as the gateway to the Xbee/Zigbee network. Programming an Arduino over Xbee has been described in detail at ladyada.net.
If you need direct WiFi connectivity this should work the same way. XBee comes in two parts: 1) the adapter, which connects to the Arduino over a serial interface, 2) the actual RF module. So if you can get a WiFi module that sits on top of a XBee adapter, the same approach should work. I haven't tried this myself, but I guess combining the first approach with the following from homebrew-tech.com should work.
Inotool is a good option. In addition to that you can also try the following two options as well.
Arduino 1.5.x only
If you are using Arduino 1.5.x then you can use the arduino executable can accept commandline parameters.
Note that Arduino 1.5.x is still in beta, so you may face some issues.
Arduino 1.0.x
If you are using Arduino 1.0.x then you can use my makefile for Arduino which can be used to compile and upload Arduino (or plain AVR C) programs to Arduino from the commandline.

Categories