Step 7: Application: talking to other modules using I²C

This is just a proof of principle and by far not the simplest solution for this job.

Using I2C allows for directly addressing up to 127 "slave" boards. Here the board on the right side in the video is the "master" (which initiates all transfers), the left board is the slave (waiting for data). I2C needs 2 signal lines and the usual power lines (+, -, SDA, SCL). As it is a bus, all devices are connected to it in parallel.

Hey madworm,That's a great instructable right there. But could you please help me in scrolling text on the matrix? I think that's the only feature missing in the instructable..
<p>Hi Madworm, great tutorial you've got here. So I've been working on implementing your circuit, but I have only arduino mega, and apparently it doesn't work there. I presume, it's something in functions that are working with SPI and timer register, that must be different for Mega. </p><p>Would you be able to point out what exactly has to be changed? I relatively new to arduino, and coding myself, so yeah.</p><p>I've tried simulation with arduino uno, and it worked fine. Mega doesn't work in simulation either..</p>
<p>First make sure you've connected the right pins. According to the datasheet [atmega1280] that chip only has ONE hardware SPI device. See where the pins MOSI, MISO, SCK and CS end up on the Arduino Mega.</p><p>Some error messages would be helpful, if there are any.</p>
<p>Yeah, I've connected data pin to 51st of arduino mega, clock to 52nd latch to 53rd. (according to second table here - http://www.arduino.cc/en/Reference/SPI)</p><p>In proteus simulation I'm getting &quot;Writing to UDR3 while transmission is not enabled&quot;</p><p>As for real circuit, I was testing just with one (or four) led(s), not entire matrix, and if I remember correctly, LED(s) just didn't do anything, and shift registers maintained constant levels on their outputs.</p>
<p>That is odd. UDR indicates involvement of an UART.</p><p>Try this: <a href="http://pastebin.com/PcjfytxE" rel="nofollow"> http://pastebin.com/PcjfytxE</a></p><p>That should &quot;blink&quot; all 4 shift registers on/off every second.</p><p>If that works, we know SPI is good.</p>
I need to look at the ATmega1280 datasheet first. I assume you've got this board (http://arduino.cc/en/Main/arduinoBoardMega).<br><br>That will take some time. It is not radically different, but I need to find the right bits &amp; pieces.
<p>Hi Madworm, I'm currently playing with this setup and do wonder one thing, Was there a specific reason to put the resistors on the cathode side? As they are all the same value technically they can be on the anode side as well, reducing the amount to only 8 instead of 24. I know it isn't the cost but it does makes the board a bit cleaner. Or am I overseeing something here?</p>
<p>Yes, there is an issue.</p><p>Putting the resistors on the anode side leads to noticeable and unpleasant brightness variations when more than one colour is turned on.</p><p>1 resistor + 1 LED --&gt; normal brightness</p><p>1 resistor + 2 LEDs --&gt; roughly half the current for each --&gt; dimmer</p>
<p>Clear, Thnx</p>
<p>Hi Madworm, thanks a lot for your instructable!</p><p>Right now, I'm trying to implement a control with a second arduino, so that the one connected to the matrix can just do that stuff (it won't get interferences from other parts of the code)</p><p>I was going to use SPI because with interrupts I can transmit all the 192 Bytes (I will reduce them when I will manage to do this). To do so, I have to use the SS pin of the Processor, which is pin 10. Now, here's the problem: I tried to modify correctly all the sketch so that it doesn't use pin 10 but pin 8. However the matrix filckers and then completely turns off. I don't know what I can do, as I need that pin to be free so that I can use it with the SPI_STC_vect interrupt..</p><p>How can I free that pin, so that I can use it for slave SPI?</p><p>Thanks</p>
<p>The AVR already uses hardware SPI in master mode to talk to the shift registers. This is quite time-critical interrupt code. I'm not quite sure how you would have it listen as a slave at the same time without disabling the matrix stuff, or if that is possible / advisable at all. Sounds like trouble to me.</p>
But isn't possible to use the the shiftOut function for the matrix instead of the hardware SPI? or it is slower?
<p>Yes and no. You can use it, but it is waaaaaaaaaaaay too slow.</p>
<p>Thank you for posting this example. It gave me inspiration to make a more complete version and write a full driver for tri color matrices while talking to them through a higher level library to draw pixmaps, letters, shapes and get scrolling text in multiple colors:</p><p>http://marc.merlins.org/perso/blog/cat/arduino/post_2015-01-06_Driver-for-direct-driving-single-to-3-color-LED-Matrices-with-software-PWM.html</p><p>https://github.com/marcmerlin/LED-Matrix</p><p>Would you mind adding a link for folks who might be looking for such a driver?</p><p>Cheers, Marc</p>
<p>Sorry for answering so late. I didn't get notifications from Instructables for a while, today I got lots.</p>
<p>Very good project!!! thanks you very much, its running perfect in my protoboard.</p><p>congratulations ;)</p><p>nice blog </p><p>best regards</p>
Hey Madworm, <br> <br>I spent all day learning about interrupts and now understand nearly all the interrupt business in your code. <br> <br>I just don't understand TIMER1_CNT = 0x0130 corresponds to 32 levels of brightness... that number is 304 no? <br> <br>because you have TCNT1 = TIMER1_MAX - TIMER1_CNT <br>so TCNT1 = 0xFFFF - 0x0130 = 65536 - 304 ? Does that mean TCNT1 is initialized to 65232, increments on every prescaler cycle till it hits 65536, starts the interrupt where TCNT1 is re-initialized to 0xFFFF-0x0130 and does the interrupt business? <br> <br>I understand this TCNTn business with the CTC mode, but I'm confused how you used it here in normal mode and how you find the value corresponding to the amount of brightness levels. Also was there a benefit to using an overflow interrupt than a CTC one? Once I understand this, I'll finally feel like I truly understand your code (After staring at it all summer). <br> <br>Thanks!! Really want to build this.
Let me look at that code... it's been a while :-) <br> <br>Ah... I see. <br> <br>OK, the purpose of preloading the timer is to have control over the frequency at which it is called. The plain overflow interrupt would take 2^16 cycles to complete, which in this case is too long. <br> <br>You see, the crude PWM generation relies on persistence of vision and uses a lot of loops in the interrupt code. These loops take some time to finish. <br> <br>If the ISR frequency is too high, all CPU time is eaten up in the interrupt, up to the point that the system appears to be frozen. <br> <br>If the frequency is too low, the display will flicker. <br> <br>The TIMER1_CNT value was determined by trial and error. The more colour-levels are to be displayed, the more time the ISR takes --&gt; TIMER1_CNT must get larger. <br> <br>Using this manual preloading vs. CTC mode... it should be equivalent. I think in later versions of the code (github) I used CTC mode. Later I switched to binary weighted PWM generation (much less CPU utilization, but it has other issues).
Ahh ok! I was thinking there was a formula or something for determining that, but when I calculated the frequency of the interrupts it was 51.23Hz, so I was thinking it was a trial and error kind of value. Now that you've confirmed that, I think I'm ready to build this thing! <br> <br>Thanks a ton!
It has been done, but it comes at a cost. <br> <br>Generating the PWM in software for true-color (sort of) takes up a big slice of CPU time. At some point, there's nothing left for anything else. If I had to do it again for a 128px display, I'd at least use dedicated LED-drivers with integrated PWM generation. A faster micro would help as well.
How about combining 2 of these, for like a game of tetris or just a larger game/screen is is possible to just hook up more ic`s and a second screen ?
Hi Im currently doing GCSE ELECTRONICS KS4 AND i WANT make a project like your one but the problem is I want to make a Arduino Clone if u guys know any step by step instructable for beginners cos im new to micro controllers.MANY THANKS
Well, the good news is that there is nothing special about making an arduino clone, no magic involved. A minimal working board is just a few parts (no on-board usb).<br><br>I don't know if you're shooting at making your own pcb with smd parts or through hole components. There's also nothing special about that, except that it costs serious money to have pcbs fabbed (more than just a few) and getting a non-functional board is quite upsetting. So you'll want to make sure you have a functional prototype of your schematic working on breadboard or vero/perf/strip-board before you shell out big money.<br><br>As you've mentioned GCSE, I take it you're in the UK. For small or one-off prototypes, there's a UK site that offers a nice service for just that ( http://www.badnetwork.co.uk/ )<br><br>As far as minimal arduino clones go, the 'boarduino' seems like a good starting point to me ( http://www.ladyada.net/make/boarduino/download.html ). Easy to build with self-sourced parts.<br><br>As far as using PCB layout software, this can be a bit tedious at first, probably even quite annoying. Personally I use KiCAD ( http://store.curiousinventor.com/guides/kicad ), which is open source and does not have any constraints like the 'free' version of EAGLE.<br><br>Also I'd like to invite you to join the arduino forum on www.arduino.cc - quite a lot of UK folk hang out there as well.
How is the total shift register (per chip) current kept below 70mA? Is this why you chose 270ohm?
Oops, just read the 1:8 duty cycle comment below.
Hi madworm,<br><br>I have one week to make an RGB LED matrix for school. How did you wire your matrix up before you transferred it all to PCB? Due to time and money constraints, I can't have a PCB manufactured. Do you have photos, schematics, or diagrams?
One full week... snigger. SCNR.<br><br>Before it was transfered to a PCB, I uses a breadboard to test the circuit. Suffice to say that it was no fun at all. So many wires...<br><br>If you go to my blog (the link is here somewhere), you will find schematics and photos (flickr) and some code as well. The best entry point is the projects page. Other posts may have outdated content. If you intend to use any of it, make sure to get the latest versions of both from the git repositories, otherwise it may have unpredictable effects.<br><br>
Dear madworm. <br> <br>Good news, I have success in having a working RGB matrix after following your codes and instruction. <br> <br>May I know how do i modify the code if I want to run it on a stand alone ATMEGA168/328 but NOT from a Arduino? <br> <br>Thank you again!
Well, once you have compiled the code (for an 168 or 328) into the .hex-files, you can just take your favourite ISP programmer and flash the chips. No need to change the source code. On linux systems the .hex files are temporerily created in '/tmp/build.xxxx', on windows I frankly don't know.<br><br>Just make sure the FUSE settings of the chips are correct. For an 168 these would be:<br><br>a) no bootloader, 16MHz quartz, 16kb usable:<br><br>lock: 0x3F<br>lfuse: 0xFF<br>hfuse: 0xDD<br>efuse: 0x01<br><br>b) with bootloader, 16MHz quartz, 14kb usable:<br><br>lfuse: 0xFF<br>hfuse: 0xDD<br>efuse: 0x00
Dear Madworm, you are great! I saw your works elsewhere in the net and I think you are the one to answer my question. <br> <br>I want to build a 5x5x5 LED matrix using RGB LED, I don't think I will have problem controlling each layer but I have 5 layers to control. I want to <br> <br>1. mix colour (not just the 7 colours) of the RGB and <br>2. contol the brightness of each and single LED <br> <br>so I think PWM (software by 74HC595 or by hardware TLC5940) is my answer. <br> <br>Do you recommend <br> <br>1. use the 74HC595 and mutliplex them for each layer (5 layers) or <br>2. Use TLC5940 and multiplex them for each layers? <br> <br>I worry that by multiplexing the chips, I do not get enough refresh rate and give rise to LED flickering...... <br> <br>Looking forward to your kind assistance. Any information would be much appreciated!!!
I'd use TLC5947 if possible. Very similar to 5940, but simpler to use. Needs no external grayscale clock, which keeps the microcontroller busy all the time. Just send the data and forget about it. With these you'll need a few extra transistors to drive the layers. Maybe something like UDN2981A if you can get that one.<br><br>The multiplexing makes it dimmer, but with professional driver chips you can compensate to some degree by adjusting the external current reference resistor. Just make sure that you never stop multiplexing...<br><br>But for color mixing anything is better than 595 chips. It may be doable, but at some point you'll wish you hadn't gone that way ;-) A whole lot of time is wasted just for generating the pwm signals.
Dear madworm <br> <br>Thanks for your kind reply. I have already received the 5947 samples from Ti and will give it a try. But exactly do i multiplex it, can you give me some direction or exact way / scehmatic that I can follow..... <br> <br>One more question, is your 64-pixel RGB matrix true color and not just the 7-color....? <br> <br>Thank you once again. <br> <br>WM Tang <br>
For starters, you can find an example for a single line of 8 RGB LEDs driven with a 5947 there:<br><br>http://docs.macetech.com/doku.php/octobrite<br><br><br>For multiplexing, you need to have a look at the BLANK input. It should be used when a new row is addressed.<br><br>A project based on the 5940 is this one:<br><br>http://www.thebox.myzen.co.uk/Hardware/Mini_Monome.html<br><br>It should convey the principle of what you need to do.<br><br>What the 5947 needs is similar. You need to compensate for the 24 vs 16 channels and throw everything out that deals with GSCLK, as this is handled internally.<br><br><br>My doodad can do more than 7 colors ;-) Technically it supports 32768 shades, but not all of them look different to the eye.
When I read the last step I laughed, so hard. Seriously man, piggyback! awesome<br>
Hey Madworm, great Instructable, just one question: I understood most of the project, but I dont get the PWM part. I understand you use th 74 595 for selecting or controlling each led and each lead of the LEDs, (1 for red, 1 for green, 1 for blue, 1 for the anode or cathode) but, I dont get how you &quot;connect&quot; the PWM to each RGB, I understood very well what PWM is, but, for example, you have in your PWM a frecuency of 1 Hz (only example), and a PWM resolution of 8 bits, and duty cycle of 25%, ok, you want to connect the output of this PWM to any lead of any led, how do you achieve that?? how do you refresh or shift data into the shift registers to achieve that?? thanks for your response, and please be a little detailed, thanks a lot...:)
There is no single PWM source that needs &quot;connecting&quot; to the LEDs. The PWM is created by sending a bit-stream to the shift registers at high speed. <br><br>If it helps you understand think of it as many virtual PWM sources that are sequentially sampled, which created the bit-stream. All of this is done in software. The result of this is then sent to the shift registers, which just reproduce it. Each SR pin is a PWM source that way.
I think I know how to get it to work with 8-bit pixel depth ... or maybe even 16-bit if you can handle a frame buffer that large. Can I get access to the driver code to try out my idea?<br><br>Also: Are the support boards for sale or can I get some made the same place you did?
You can grab the code and _all_ design files (KiCAD + schematic + gerber files) on my blog ;-) The link should be on my page somewhere here.<br><br>Using the gerber files, you can have the boards made for you anywhere. If you just need one board (no parts), this shouldn't be a problem as well ;-) I will find one in my many boxes.<br>
(Trying again, code got garbled the first time.)<br><br>OK I have the code. I am new to Arduino so I am having trouble seeing where the duration of a light pulse is set, but let me describe the idea here in words and pseudocode.<br><br>Instead of a countdown loop with &quot;&lt;&quot; comparisons, we look at each bit of the brightness from MSB to LSB. (I will here assume 5 bit depth but the generalization should be obvious.)<br><br>In pseudocode (borrowing from the actual code):<br>#define __brightness_bits 5<br>#define __brightness_levels (1&lt;&lt; __brightness_bits)<br>...<br>for (bit=(__brightness_bits - 1);bit&gt;=0;bit--)<br>{<br> mask = (1 &lt;&lt; bit);<br> for (led = 0; led &lt;= __max_led; led++) {<br> if (mask &amp; brightness_red[row][led]) {<br> red &amp;= ~(1&lt; }<br> if (mask &amp; brightness_green[row][led]) {<br> green &amp;= ~(1&lt; }<br> if(mask &amp; brightness_blue[row][led]) {<br> blue &amp;= ~(1&lt; }<br>}<br>// Now here comes the key idea:<br>// Drive the LEDs for time proportional to (1&lt;&lt; bits)<br>drive_LED_array_for_time(mask);<br>}<br><br>The number of different times the LEDs get driven here is proportional to __brightness_bits, not 2 to that power, so in this case 5 (not 32). It scales nicely, linearly instead of exponentially. The remaining problem is simply how to drive the LEDs for a variable amount of time. This might be easy for you, but I need to understand the Arduino interrupt system first, which will take me a while.<br><br>Does this make sense?
'Simply' is such a nice word ;-)<br><br>At first glance I feel you have done this:<br><br>a) remove computational needs from the 'decision making' process, better scaling law: O(n)<br>b) add complexity to the 'driving the LEDs' part<br><br>When driving just a single LED (or many in the same way) it would be easy to off-load this task to one of the timers in PWM mode.<br><br>For generating the required PWM bit-pattern with shift registers you'd have to run some sort of loop again I think, or use dedicated driver chips with integrated PWM counters (like TI5947, not exactly cheap).<br><br>Maybe this is a zero-sum situation, but I can't tell for sure.<br><br>Let's see your 'make the LEDs do what I need' code ;-)
I think this should be pretty easy once I get up the learning curve. It is possible to set an Arduino up so that a timer drives PWM directly without burning CPU cycles; see for example the audio driver at: <a href="http://www.arduino.cc/playground/Code/PCMAudio">http://www.arduino.cc/playground/Code/PCMAudio</a><br> I think this is what you meant by &quot;off-load this task to one of the timers in PWM mode&quot;. So the right SW architecture here is to use one timer in PWM mode just for display on time. Within each frame, you fire off the PWM timer in <strong>__brightness_bits</strong> groups * <strong>__rows</strong> rows/group with a different timer setting for each group, but you don't need to sit and loop inside the interrupts and waste a ton of CPU time. Just load the display bits and the timer and then exit: &quot;Fire and forget.&quot; :-) You do of course have to know whether it's finished so you can fire off the next one, but that can either be done off the timer interrupt, or, less precisely, just as part of your main loop where you are polling buttons or whatever else you do in the unused CPU cycles. Assuming the minimum pulse width is one clock cycle, the total driving time is still at least <strong>__max_brightness</strong> clock cycles plus overhead. So we are constrained by<br> &nbsp;&nbsp;&nbsp; <em>frame_rate</em> * <em>total_driving_time</em> &lt; <em>CPU_clock_speed</em><br> so assuming 16 MHz CPU and 1000 cycles overhead per depth-bit per frame (125 cycles per row display) we get theoretical max frame rates of roughly:<br> <br> &nbsp;&nbsp;&nbsp; 4-bits =&gt; 3984 Hz (16000000 / (4*1000 + 16))<br> &nbsp;&nbsp;&nbsp; 5-bits =&gt; 3179 Hz (16000000 / (5*1000 + 32))<br> &nbsp;&nbsp;&nbsp; 6-bits =&gt; 2638 Hz (16000000 / (6*1000 + 64))<br> &nbsp;&nbsp;&nbsp; 7-bits =&gt; 2244 Hz (16000000 / (7*1000 + 128))<br> &nbsp;&nbsp;&nbsp; 8-bits =&gt; 1937 Hz (16000000 / (8*1000 + 256))<br> &nbsp;&nbsp;&nbsp; 9-bits =&gt; 1682 Hz (16000000 / (9*1000 + 512))<br> &nbsp;&nbsp; 10-bits =&gt; 1451 Hz (16000000 / (10*1000 + 1024))<br> &nbsp;&nbsp; 11-bits =&gt; 1226 Hz (16000000 / (11*1000 + 2048))<br> &nbsp;&nbsp; 12-bits =&gt; 994 Hz (16000000 / (12*1000 + 4096))<br> &nbsp;&nbsp; 13-bits =&gt; 755 Hz (16000000 / (13*1000 + 8192))<br> &nbsp;&nbsp; 14-bits =&gt; 526 Hz (16000000 / (14*1000 + 16384))<br> &nbsp;&nbsp; 15-bits =&gt; 334 Hz (16000000 / (15*1000 + 32768))<br> &nbsp;&nbsp; 16-bits =&gt; 196 Hz (16000000 / (16*1000 + 65536))<br> &nbsp;&nbsp; 17-bits =&gt; 108 Hz (16000000 / (17*1000 + 131072))<br> &nbsp;&nbsp; 18-bits =&gt; 57 Hz (16000000 / (18*1000 + 262144))<br> &nbsp;&nbsp; 19-bits =&gt; 29 Hz (16000000 / (19*1000 + 524288))<br> &nbsp;&nbsp; 20-bits =&gt; 14 Hz (16000000 / (20*1000 + 1048576))<br> <br> Note that for under 14 bits, the overhead is most of the time, while for 15 and over, the displaying time dominates. But anyway it seems like 16-bit pixel depth should be achievable at completely flicker-free frame rates (if we have room for the double-size frame buffer). Even assuming the overhead doubles for 9-16 bits, we still get 16-bits =&gt; 164 Hz. 8 bits should be easy. One could either stick to a fixed frame rate (using the other timer) or just let this run as fast as it will go for maximum brightness (but with possible variations in brightness if the timing varies due to other loads). With this architecture the CPU overhead at 5-bit depth probably drops from 50% to around 1%. Give me a few days to receive the Arduino Uno I just ordered and a week to play around with it and I'll have code for you.
Hm, wouldn't there still be an awful lot of switching between the groups? Worst case: each LED has unique values for every color. I think getting a dedicated driver chip (e.g TLC5947 24-ch PWM driver) would be much simpler - if it were available right now. Out of stock everywhere. Only the switching between the rows would remain.
I need to order an Arduino first I think. As far as you know, will your board work with the Uno?<br><br>I don't think it's zero-sum, I think it's a clear win, but the proof would of course be running code. It might be as simple as changing the how-long-to-drive-a-single-row constant to be a variable.<br><br>A free PCB would be lovely.
It's not a shield, but an Arduino-clone of its own accord. You could abuse your Uno to program it though.
that' an tv
I should work this into a game of life algorithm.
Is there any visible flicker?&nbsp;

About This Instructable




Bio: Slightly Dorky High Nerd - You might find some of my stuff on Tindie.
More by madworm:IKEA Samtid mood-light upgradeIKEA Samtid reading light upgradeBreadboard LED indicator
Add instructable to: