The Cheapest Microcontroller? Getting started with the 10 cent Puya PY32.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
it's 2024 and well 10 cents just doesn't buy you much anymore you can get about four fluid ounces of gasoline maybe one half a one hamburger BN half a gigabyte of SD card space from a reputable manufacturer a minute and a half at Disney World which you'd almost certainly spend waiting in line the point is 10 cents doesn't go far but 10 cents can buy you this this is the Pua py 332 a full FL Ed 32-bit reprogrammable flash-based microcontroller with a cortex m0 plus core now if we're really being semantic I paid a smidge over 11 cents for this one but there are other package options that are available for under 10 cents each if you buy 500 no matter how you slice it these things are cheap in this video I'm going to give you a brief overview on these chips and then walk you through setting up the software development environment I'll cover the tools I'm using to program these and then I'll flash a couple of test programs to show you that these do in fact work as described I'm Zack and you're watching Zack of all [Music] trades the point of this video is to show you how easy it is to get started with these not to be an in-depth guide I am not a microcontroller expert or embedded software for that matter but that should be encouraging to you because if I can do this you can too 2 first I'm going to spend a few minutes discussing the py 32 in general if you're already familiar with these jump to the getting started with microvision Section so when would you want to use a 10-cent microcontroller while saving money would be great all the time these do have limitations for one 10 cents buys you a bare chip you need a circuit board a programmer a couple of passive components and then you need to solder it all together pya does have some development boards available but they're very basic without built-in debugging and they're a bit pricey for what they are additionally these are not powerful chips certainly faster than say an original Arduino but not compared to some of the other 32-bit Parts out there there are going to be cases where these are just too slow or they don't have all the features you need additionally the software tools and online support are pretty much non-existent to develop with these you'll need to be comfortable researching issues yourself and capable of parsing data sheets and reverse engineering the provided example projects so most of the time if you're building one or two of something you're much better off just buying a fully featured development board something like an Arduino or Raspberry Pi Pico any of the popular options these have wellestablished development tools plenty of documentation and lots of online discussion but what if you need to make a a lot of something let's compare this 10cent py 32 to the cheapest stm32 part which is around 70 cents or the 80 cent but far more powerful rp240 from the Raspberry Pi Pico take the example where you're creating your own product that needs a small microcontroller if you hope to sell 10,000 items then using this 10cent part versus the 70 or 80 cent part will put about $6,000 more profit in your pocket and this of course scales linearly so if you're going to make a million of something using this would put $600,000 more in your pocket this is why companies spend loads of time optimizing their bill of materials cost even just to save fractions of a scent on a part the benefit I see for those of us not selling things is that having a chip this cheap allows us to build projects that need hundreds of microcontrollers say you're needing to detect something like temperature s round maybe motion across a large area using hundreds of sensors you may be able to find a smart sensor with an interface that would allow this but if that doesn't exist or if it's cost prohibitive then you could take one of these cheap microcontrollers and turn that dumb sensor into a smart one you can add custom processing at the sensor to perform data simplification Say by detecting a temperature threshold being crossed or perhaps performing a foilage transform on a signal and just returning the frequency content using this you could do most of the processing at each sensor and only notify a larger Central processor if something interesting occurs this will greatly reduce the demand on the bigger Central processor this approach is exactly what I'm doing on my LED tile backsplash project I'm making several videos on this and they're Linked In the description be sure to subscribe to stay at to dat on this project I'm using these p i32 microcontrollers to read a microphone signal and then calculating the largest signal amplitude and then returning that value to a central processor at a specific time nothing was available off the shelf and I needed 150 maybe 200 of these so whatever I did had to be cheap or this would have been way too expensive for a DIY project another potentially big benefit of the py 32 is that it natively runs at 5.5 volts not just the 5vt tolerance inputs you get on some of the other microcontrollers this means you can largely do away with level shifters although I should note that this does come at the expense of a little higher power draw I also feel like I should note that this isn't the only extremely cheap microcontrol out there there are Parts as low as three cents although these tend to be 8 bit parts and onetime programmable there's also the ch32 v003 this is a risk five part that is a around the same cost and actually seems to be more popular there are more development board options for these but bare chips seem Harder To Source LCSC currently only has one option in stock if you go to LCSC to buy the p32 you'll find four options of the F0 variant available F0 here just means that they cortex m0 plus parts the higher the part number the more features you get and also the more you're going to pay the f2a the cheapest part can run up to 24 MHz has 20 KOB of flash 3 kiloby of SRAM up to 18 IO one analog to digital converter three timers and one each of spy uart and i2c notably it does not have direct memory access dma or a real-time clock the f00003 adds the dma and the real-time clock and it allows operation up to 32 MHz it also has more Flash and RAM and an extra uart but expect to pay more these start at 14 talk about pricing going up to the f030 gets you all the above but now with six user configurable timers an extra spy bus 32 kilobytes of Flash and 4 Koby of ram these can be run up to 48 MHz and you even get internal phase locked Loop to produce this frequency from the internal high-speed oscillator these will set you back around 30 cents and finally the f72 the real powerhouse 128 Koby of flash 16 Koby of ram running up to 72 MHz with 10 timers you even get a DAC and for connectivity four orts 2 i2c two spy with i2s for audio one can even USB there's a 64 pin package option available that has 58 iio these do cost around 50 cents if you're buying 500 though and that's pushing the RP 2040 territory so I personally think the lower-end py 32 Parts the really cheap ones are the more compelling candidates now I've read that the f002 and the f030 are actually the same die inside and if you know what you're doing you can uncover that extra memory the PLL and the higher clock speed the dma the other peripherals I haven't verified this but I'm planning to build some general purpose boards in the future and I hope to check this out to develop code program and debug these devices you'll want an integrated development environment choice of IDE is hugely opinionated but in an effort to get the py 32 up and running as quickly as possible I chose to use Keel micro Vision as puya devices directly support this there are other Ides you can use I've seen people using stm32 Cube IDE even and there are other tools included with the documentation but I've yet to explore these besides an IDE you'll need something to program with I found that my generic St link V2 works well and I've put an affiliate Link in the description for these there are lots of other options out there as well you can even make debuggers using common microcontroller boards you're also probably going to want something for serial communication with a computer I'm using a second St link a V3 in this case but any rs232 serial adapter will work fine you're probably also going to want a py 32 microcontroller board to check out your code and this is perhaps the trickiest part Boya has start kits available that are really nothing more than a chip broken out to the pins with a crystal a regular later and a couple buttons there's no onboard programmer debugger when I bought my p32 f003 version this was about 3 or 4 months ago these boards were $5 each but now they're up to $185 which is frankly ridiculous for what you're getting obviously if you're wanting to check out the p32 paying $18 is way easier than trying to spin your own board but come on $18 for a 10-cent microcontroller regardless I'll be using the p32 f003 start kit in these examples so to get started download and install micro Vision there are fre versions available straight from arm that are plenty sufficient also you're going to want to get the documentation zip file from PUO extract it and just have a poke around him there now microvision does not like Chinese characters in The Path name so use this opportunity to rename them anywhere you see them when you open microvision the pack installer should automatically load if not open it wait for it to update and puya should appear on the left hand side drop the menus down find the appropriate device and then click install at the top of the right side and that's it for installing the pack now create a new project in microvision in the dialogue select the appropriate pack you've loaded I'm using the puya p32 f003 X6 here you're going to want to select CM CIS cor and the device startup now I'm going to load the template code that puya provides in their documentation go to libraries and examples and then drill down to templates select your board and you should find source and header files there will be a main source file and a main header an interrupt source file and header a how MSP source and a how config header you don't need to recopy the system source file microvision already has it I recommend copying these files to the local project folder since you're likely going to be changing them I'll put them in objects in microvision right click on the source group and then choose add existing files to group then navigate and load the appropriate files you just moved over next we'll need to set the defines for the project to tell the code which microcontroller we're dealing with right click on target one then choose options for Target go to the c/c++ tab and enter your microcontroller into the Define box I'm using the p32 f00 3x6 so that's what I'll put here while we're here there are a couple of include paths you might want to add the first is just to the objects folder within the project directory the second is for the header files for the how drivers we'll cover this more later but just open the stuff you downloaded from GitHub navigate the path I'm showing here and click the include folder finally under the debug tab you can select the debug probe you're using and configure any other debug settings you might want I'm using the stlink V2 now since I want to use the how drivers I'm going to create another source code group and add the how driver source code there may be an easier way to do this in microvision let me know if there's a better way in the comments but what I'm to do here is simple enough I'll just add existing files then navigate to libraries and examples click through then drivers and p32 f0xx hore Driver then I'll go to Source I'm just going to add all the how ones and skip the low-l drivers you could also only grab the ones you need if you're so inclined okay now that everything is set up time for a couple of quick examples first the hello world's embedded devices blinking an LED I'm going to do this with a timer interrupts rather than delays since delays lock the processor while they're executing first I need to configure the output pin that drives the LED looking at the schematic for this board the LED is connected between pin pb5 and vdd with a current limiting resistor I've written a function to initialize this I'll just set the pin set it push pull pull it down and initialize it on Port B I want the LED to toggle periodically so now I'm going to set a timer with that period I'll create a timer initialization function and for this one I'm going to use timer one which is an advanced timer I'm not going to get into timer setup in depth but I want this to trigger four times a second or every 250 milliseconds running the clock at 24 MHz means I want the timer to trigger every 8 million Cycles so I'll set the period to 10,000 and the prescaler to 800 these are zero indexed so be sure to subtract one you can search stm32 timer setup to get more information on this now I'll just call the initialization function make sure to use the interrupt version that's what the it here means also don't forget to go into the how configuration header and turn the timers on by uncommenting the appropriate Define now I need to tell the microcontroller what to do when the timer interrupt triggers this code needs to go in the Callback Handler for the timer this function here every time it triggers I simply want to toggle the LED so I'll use the gpio toggle function I also need to add a function to the interrupt file the p32 f0xx it file to handle the interrupt as shown here make sure to add the prototype to the header file as well for clocking I'm going to change the default 8 MHz to be 24 MHz just by changing the HSI calibration value lastly I'll go to the halor MSP file and I'll add this how Tim base MSP init function this is going to enable the timer clock as well as configure and enable the interrupt throw the two initialization functions into the main function and that should be it let's Flash the code two little hints here one is that even though the part will run at 5 volts I cannot get the code to flash at that volt voltage to program the parts I'm using 3.3 volts as generated by the St link the second thing is that the microcontroller needs to be restarted after flashing the code and this isn't automatically done at least for me there's a reset button on the start kit or you can just unplug and replug so load the code done reset and there we go flashing light okay now I'm going to do something slightly more interesting I'm going to use the internal ADC to read an analog voltage coming from a potentiometer then I'm going to send the resulting value to a computer over the UR peripheral this is the exact sort of thing You' do if you were making an analog sensor into a smart one the first thing I did was write three configuration functions let's start with the gpio I looked at the data sheet and found ur2 on pins pa0 and pa1 which is alternate function nine for those pins I put the struct together and I initialized it I separately set up the ADC using Channel 2 on pin pa2 I set it to analog and I initialized that one as well next the uart I'm going to set everything up fairly standard as far as bits are concerned I set the B rate to 115,200 any standard value should be fine here just make sure your receiver is set the same I then called the uart initialization function finally on to the ADC there's a lot of configuration that can be done here but without going into too much detail I'm just going to do 12 bits a single conversion software start 41 1 12 cycles per conversion I'll call the ADC initialization function next I need to set the rank I'm going to set it to the highest and then I'm going to set the channel to channel two finally I'll call the appropriate configuration function if this is looking unfamiliar there are lots of other tutorials online on setting up the ADC or just reference the stm32 how manual now I'll head over to the main function and get everything working first I'm going to call those three functions I just wrote now I'll call the ADC in polling mode and I'll save the value to a variable once I have the binary value I will go ahead and convert it to a string I'll then concatenate it with some other texts so that it reads the ADC value is blank now just call the UR transmit just going to do this in blocking mode finally I'll add a 1se second delay and then have this just repeat over and over for circuit setup I'm using a potentiometer with a center tap run to the ADC pin I've then run the uart to an stlink V3 which has a Serial transceiver built in but any USB to serial adapter should work fine for this I've opened a Serial console in Moa exterm with the appropriate B rate now I'll just flash the code reset the board and there we go as I turn the potentiometer the reading increases and decreases as it should there are of course other peripherals built into this thing but there are a couple of places you can go if you need more help for one the example code provided in the zip file from puya is very useful and it covers pretty much every peripheral there's also a well-written reference manual for the f030 in English although don't expect step-by-step guidance finally these parts are really similar to stm32 Parts I dare say puya borrowed a little from St therefore the stm32 Hal manual is directly applicable and it goes into great detail I've been using the one for the stm32 g0 series which is also a cortex m0 plus core little hint here not all of the features available on the stm32 g0 exist on the p32 so if you're configuring a peripheral start by typing all of the configuration parameter names in microvision if it doesn't attempt to autocomplete then it probably doesn't exist on the p32 chip if you do type it all the way out and it doesn't exist it'll also throw an error such as no member found that's pretty much all there is to programming these if you've coded cortex m0 Plus or other similar Parts before you'll find this very familiar there really aren't any surprises the hardest part honestly is probably designing a circuit board and soldering the chip to it I've posted both of these simple projects to GitHub Linked In the description if you've got any specific questions go ahead and post them in the comments and I'll do my best to answer them that's all I've got today if you found this useful please like And subscribe and go check out my other videos where I put the p32 to use thanks for watching
Info
Channel: Zach of All Trades
Views: 56,080
Rating: undefined out of 5
Keywords: Puya, Puya PY32, PY32, cheap microcontroller, electronics, cheapest, cheapest microcontroller, CH32V003, 32-bit microcontroller
Id: qWRowOsYSLY
Channel Id: undefined
Length: 21min 35sec (1295 seconds)
Published: Wed Mar 27 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.