Using a Raspberry Pi to Program an AVR and Python SPI Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this tutorial I'm going to demonstrate how to use a Raspberry Pi to program AVR chips also introduce how to use SPI protocol in Python which can be used to communicate between a pie and an AVR or any other device that supports SPI I've been working with that melody our chips for a long time and my website contains many AVR based projects such as a CNC joystick wake on land RGB LED fader timers inductance meter recoil gun and much more an AVR is a very powerful microcontroller it offers much of the functionality of a Raspberry Pi and in some cases more features a microcontroller is a small computer on a single chip the 18 mega and 80 tiny AVR ICS can often be purchased on eBay for a couple dollars since they're so small powerful and cheap they're often great solutions to control your projects and the use of a VRS by Arduino has also made them very popular AVR chips run code that can be compiled from basic C and assembly language the code is usually written on a PC and then compiled and uploaded to the AVR with a programmer Adafruit sells a USB programmer kid that works well I also have a USB programmer that I developed the plans are free on my website here you can see me using mine to upgrade the firmware on my CNC joystick of course you don't need to buy a programmer if you have a Raspberry Pi because it can do the job for this demo I'll be using an 80 tiny 24 a VR chip it's a 14 pin dip package so it's breadboard friendly 12 pins can be used as inputs or outputs similar to the PI's GPIO eight of these pins are hardware ADC which means they support analog to digital conversion you can use them to measure voltage or read analog devices the feature missing from the PI this AVR can run at a voltage between one point eight and five point five volts so it can be powered by the PI it can be clocked up to 20 megahertz and it has a universal serial interface or USI which is compatible with the PI is SPI SPI stands for serial peripheral interface it's often pronounced spy it's a very common protocol for transferring data between devices it requires three wires mossie which is the master output slave input it handles output from the master my cell which is the master input slave output it handles the output from the slave cereal clock is generated by the master and synchronizes the timing between devices if you have more than one slave than a chip enable line also called slave select is required for each additional slave for this example we'll keep it simple - one slave so we need to worry about the extra chip enable lines the wiring to program an AVR from the PI is very simple there are 4 GPIO connections the mossie my sow and serial clock pins of the pie and the AVR will be connected together an additional pipe in will control the AVR reset 1k ohm resistors are placed in series with each connection to limit the current the PI will also provide 3.3 volt power and the ground on the breadboard I have an 80 tiny 24 a VR connected to the VCC and ground rails I'll add a 1000 ohm resistor to pin 4 of the AVR which is the reset pin then I'll patch the other side of the resistor to GPIO 12 on the PI you can use any pipe in for reset some people prefer to use GPIO 8 which is the SPI chip enable but that could cause issues later when we start using the actual SPI protocol next another 1k ohm resistor is added to pin 7 of the AVR which is mossy the other side of the resistor is connected to GPIO 10 which is the corresponding mossy pin on the PI another 1k ohm resistor is added to AVR pin 8 which is my so the other side is then connected GPO 9 which is the PI's my so 1/4 1000 ohm resistor is added to AVR pin 9 which is a serial clock this one then gets passed to GPIO 11 which is the PI serial clock finally I'll provide 3.3 volt power from the PI to the AVR and hook up the ground this ensures that the voltage on the data lines is compatible if the AVR was running at a higher voltage then we'd have to use a level shifter in each data line to protect the PI's GPIO pins which can tolerate more than 3.3 volts ok that's all it takes in terms of hardware next we need to install the programming software which is called AVR dude there are three dependencies bison and flex for parsing and live USB dev they are installed using apt-get install W get is used to download AVR dude and as of this video 6.1 is the latest version once downloaded extract the turbo and CD to the newly created folder ABR dude requires manual installation because GPIO support is not installed by default configure is used to specify an able Linux GPIO this option will allow using the pinus GPIO from with an AVR dude the summary shows that the Linux GPIO is enabled use the make command to compile the AVR dude software and then make install is used to install the AVR dude there are a few required settings so open the AVR dude configuration file with Nano press ctrl W to bring up the search input and then find the Linux GPIO section which governs the pins used for programming i'll uncomment this section and then specify the pins 12 for reset 11 for serial clock 10 for mossie and 9 for my so ok that's it for the configuration I'll save the changes and exit here's a simple AVR test program I wrote in bascomb basic for the at2020 for running at 1 megahertz I'll just briefly go over the main program loop so you can see how it works get a DC will pull a potentiometer connected to AVR 80 spend 0 that value will be scaled to 3 bits so there will be 8 possible values 0 to 7 which will be translated to an RGB color finally an RGB LED connected to the AVR will be set to that selected color basically the pot will control the color of the LED i define the 8 colors using a small RGB array the test program will require a few additional components a 10k ohm resistor will pull the reset pin high the potentiometer is added that will vary voltage between 0 and 3.3 volts based on the dial setting and an RGB LED will be added with resistors to limit the current back on the breadboard I'll add a potentiometer and connect it to a VR pin 13 which is a DC 0 this pin can read analog voltage values and convert them to a digital number next I'll place an RGB LED on the breadboard which can display any color depending on how the pins are power the common cathode is connected to ground a 330 ohm resistor connects a VR pin 2 which is port B 0 to the green add own of the LED another 330 ohm resistor connects a VR pin 3 which is port b1 to the blue a node of the LED and a final 330 ohm resistor connects a VR pin 5 which is port b2 to the red anode finally a 10 thousand ohm pull-up resistor is added between AVR reset pin and VCC to ensure the reset pin does not float that's all the hardware we need to run our first test I created a folder in my home directory called AVR and place the compiled hex code for my basic test program the hex is the machine code that the AVR will run it's compiled from the higher level code using developer tools such as atmail studio or bascomb AVR I'll use AVR dude which is located in the opt a mayor dude bin to upload the code to the AVR the piece which specifies t24 for the 80 tiny 24 the c s-- which specifies the programmer which is Linux GPIO and new flash W specifies writing the hex file to the chip flash memory the verification shows that it worked the pot is rotated all the way clockwise and the LED is illuminated red when I turn the dial counterclockwise colors will start to change the AVR reads the different voltage is sent by the pot and outputs a corresponding color program is running independently of the PI but suppose we want a program that requires the pie and the AVR to communicate to implement this feature will use spi the pile will be the master and the AVR will be the slave spy protocol allows us to transmit bytes between two devices every time a byte is sent from the master the slave can simultaneously return a byte both devices have a memory location containing a byte and the protocol basically swaps the two bytes this provides bi-directional communication before using spy we have to enable with raspy config select Advanced Options then spy select YES for would like the spy interface to be enabled hit OK a reboot is required and yes for the spy kernel module to be loaded by default ok press finish and now reboot after reboot LS mod grep spy should show spy underscore DCM to 7:08 another test is to open boot config text at the end of the file there should be DT program equals spy equals on the Python spy module requires Python 2.7 dev which I'll install with apt-get install the Python spy library we'll be using is called PI spy dev and I'll use git to clone it to my home directory I'm using the das team fork which as of this video appears to be the most current version CD to the clone directory and use Python setup to install it I'll use GK su to launch Idol because we will need super user privileges here's a small Python program to test the spy interface the PI spy dev module that we just installed it's imported instantiated and opened I'll throttle the maximum communication speed to 250 thousand Hertz the main program loop will cycle through seven colors the spine expert to command is used to transmit the current color from the pi to the AVR and at the same time a response is returned from the AVR to the pi this response will be printed to the console when the program ends the spy connection is closed here's a new basic program for the et tiny 24 again running at one megahertz AVR pins a 5 a 4 and a 6 are configured for spy we're using the exact same wiring from the previous test the same wiring that allows us to flash the AVR also allows us to implement spy communication the main loop checks a flag determined if any data has been transmitted from the pi if so the flag is reset and the data is passed directly to port B which controls the RGB LED pins the LED will change to the color sent from the PI next the pot connected to a DC 0 is pulled the results are scaled to a byte value 0 to 255 that byte is then placed into the USI data register which causes the value to be sent back to the PI during the next cycle in short the two programs do two things allows the PI to change the color of the LED and it allows the pipe to read the value of the potentiometer I'll use AVR dude to transfer the newly compiled hex file to the AVR verification is okay after using a VR dude it's important to reload the spy module RM mod removes module then modprobe reloads module if you're experiencing any unexplained SPI problems there's a good chance that reloading will help I'll run the Python program now on the breadboard you can see the LED changing colors the console is displaying zeros because the potentiometer is turned to zero volts now move the pot dial to the center the console value is changed to 129 about halfway between 0 and 255 if I turn the pot all the way to 3.3 volts the console values change to 255 the 0 to 3.3 volts is translated to a byte between 0 and 255 one pitfall I encountered during testing was transmission errors I traced this down to speed by default the 80 tiny runs at 1 megahertz this saves energy but it's a little slow for SPI now this example it doesn't really matter but often speed is important to a design it's easy to bump up the AVR speed first I'll change the crystal setting in the AVR program from 1 million to 8 million and recompile the hex code this raises the AVR speed from 1 megahertz to eight megahertz now in the Python code I can remove the max speed property which was throttle in the SPI speech 250 thousand Hertz this was necessary to prevent transmission errors at the previous 1 megahertz AVR speed I'll use AVR do to transfer the hex code with the increased speed setting to the AVR however coat changes are not enough there's a hardware fuse on the AVR that also has to be changed there's an online tool that facilitates this process just Google ABR fuse and the first result should be this ABR fuse calculator you just specify your AVR chip which is at2020 for here's the default view settings which show an 8 megahertz clock but it's divided by 8 so it's really only 1 megahertz the current settings translate to a low fused byte of hex 62 I'll go back up and I'll turn off the divided by 8 checkbox which will raise the speed to 8 megahertz now the little fuse byte has changed from 62 to e2 the fuses are set with the programmer using AVR dude and this calculator is nice enough to provide the AVR dude syntax which you can just copy and paste currently our loaf used by it is set to 62 which corresponds correctly with the online tool I'll run a bar dude with lower fuse right parameters I copied from the online tool the fuse is verified and now the lower fuse reads e 2 instead of 62 again it's important to reload the spy module after programming please be extra careful when programming fuses it's very much like upgrading the firmware on any hardware if you make a mistake you can brick the AVR chip I'll run the Python code without the speed throttle the console is showing 127 which matches the centered potentiometer if there were errors the console would throw out some random numbers other than 127 looks like no errors I hope you found this video useful you can support this channel by subscribing or leaving a like all the code diagrams and more for this tutorial are available on my website roto tron info thanks for watching
Info
Channel: rdagger68
Views: 42,558
Rating: undefined out of 5
Keywords: Raspberry Pi (Computer), Serial Peripheral Interface Bus, Python (Programming Language), Atmel AVR (Computer Processor), Bascom, isp programmer, AVRDude, LinuxGPIO
Id: npSwLOMfstY
Channel Id: undefined
Length: 14min 23sec (863 seconds)
Published: Fri Jul 03 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.