<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Ardunio on davepedu.com</title><link>https://davepedu.com/tags/ardunio/</link><description>Recent content in Ardunio on davepedu.com</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><managingEditor>dave@davepedu.com (dave)</managingEditor><webMaster>dave@davepedu.com (dave)</webMaster><lastBuildDate>Tue, 17 Mar 2015 00:00:00 +0000</lastBuildDate><atom:link href="https://davepedu.com/tags/ardunio/index.xml" rel="self" type="application/rss+xml"/><item><title>arduino pong, part 1: the display</title><link>https://davepedu.com/blog/2015/03/17/arduino-pong-part-1-the-display/</link><pubDate>Tue, 17 Mar 2015 00:00:00 +0000</pubDate><author>dave@davepedu.com (dave)</author><guid>https://davepedu.com/blog/2015/03/17/arduino-pong-part-1-the-display/</guid><description>&lt;p&gt;As mentioned in my last post, I&amp;rsquo;ve been learning up electronics with the Arduino. I found a few 5 by 7 LED matrices at a
local surplus store, pictured below, so I figured another fun beginner project would be creating a clone of pong using a
few of these for the screen.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://davepedu.com/blog/2015/03/17/arduino-pong-part-1-the-display/img/led_matrix2_hu_1aba5875d30003ac.jpg" alt="5 by 7 LED matrix"/&gt;&lt;/p&gt;
&lt;p&gt;After some quick research - googling the part number - I found a datasheet, read it, and learned that the LEDs in these
units are arranged in a grid pattern, with rows sharing anodes and columns sharing cathodes. Meaning, to light up all
the LEDs in a single row, we provide power to only that row and ground all seven columns. The way this is designed, we
can decide what LEDs we want lit in only a single row (or column) at a time. So, when an image is displayed using the
entire matrix what actually happens is a single column/row is lit up at a single instant and this is applied to reach
row as are scanned down through hundreds of times per second, so it looks to the human eye that the LEDs are on
constantly. This is called the persistence of vision effect. Another nice advantage of this is power savings - even
though an LED in the grid appears to be on constantly, it&amp;rsquo;s actually only on for 1/5th of real time (or 1/7ths depending
on how you want to use the matrix - scanning by row or column).&lt;/p&gt;</description><content>&lt;p&gt;As mentioned in my last post, I&amp;rsquo;ve been learning up electronics with the Arduino. I found a few 5 by 7 LED matrices at a
local surplus store, pictured below, so I figured another fun beginner project would be creating a clone of pong using a
few of these for the screen.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://davepedu.com/blog/2015/03/17/arduino-pong-part-1-the-display/img/led_matrix2_hu_1aba5875d30003ac.jpg" alt="5 by 7 LED matrix"/&gt;&lt;/p&gt;
&lt;p&gt;After some quick research - googling the part number - I found a datasheet, read it, and learned that the LEDs in these
units are arranged in a grid pattern, with rows sharing anodes and columns sharing cathodes. Meaning, to light up all
the LEDs in a single row, we provide power to only that row and ground all seven columns. The way this is designed, we
can decide what LEDs we want lit in only a single row (or column) at a time. So, when an image is displayed using the
entire matrix what actually happens is a single column/row is lit up at a single instant and this is applied to reach
row as are scanned down through hundreds of times per second, so it looks to the human eye that the LEDs are on
constantly. This is called the persistence of vision effect. Another nice advantage of this is power savings - even
though an LED in the grid appears to be on constantly, it&amp;rsquo;s actually only on for 1/5th of real time (or 1/7ths depending
on how you want to use the matrix - scanning by row or column).&lt;/p&gt;
&lt;p&gt;I wanted a bigger screen than a single 5 by 7 matrix, so I&amp;rsquo;m stacking two right next to each other to form a 10 by 7
display. Since we now have 10 rows, we have 10 input pins. I can tie the ground (output) connection of correlating
columns together so the entire screen still has only 7 ground pins. So, however we choose to run the screen&amp;rsquo;s logic, we
will need to &amp;ldquo;push&amp;rdquo; 17 bits to the screen - 10 to describe the input state, and 7 to describe the output state. Let&amp;rsquo;s
focus on setting that up first.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://davepedu.com/blog/2015/03/17/arduino-pong-part-1-the-display/img/chips2_hu_39a7f5248c89a035.jpg" alt="Microchips I&amp;rsquo;ll be using"/&gt;&lt;/p&gt;
&lt;p&gt;I decided to use a 74HC shift register, pictured on the right above. This chip provides 8 outputs that we can control
using just three inputs. The shift register has an internal state, known as a register. When we &amp;ldquo;push&amp;rdquo; bits into the
chip they are added at the beginning of the register, and existing bits cascade down (bit 0 moves to position 1, bit 1
moves to position 2, etc. The bit in the last position is dropped). Our data bits are signaled at pin 14, known as &lt;code&gt;SI&lt;/code&gt;
or &amp;ldquo;serial data input&amp;rdquo;.&lt;/p&gt;
&lt;p&gt;To tell the chip that there&amp;rsquo;s a bit waiting on &lt;code&gt;SI&lt;/code&gt;, we change pin 12 (&lt;code&gt;RCK&lt;/code&gt; or &amp;ldquo;storage register clock input&amp;rdquo;) from 0v
(low) to 5v (high). The chip recognizes this change and copies the single bit from &lt;code&gt;SI&lt;/code&gt; to the internal register. Then,
when we want the chips&amp;rsquo; 8 output pins to reflect the state of the internal register, we simply change pin 11 (&lt;code&gt;SCK&lt;/code&gt; or
&amp;ldquo;shift register clock input&amp;rdquo;) to high. Again, the chip recognizes this change and updates the output.&lt;/p&gt;
&lt;p&gt;There&amp;rsquo;s also pins to clear the register or toggle the chips ability to output anything at all but for we don&amp;rsquo;t need that
functionality. They can be ignored by either tying them to ground or a 5v source, whichever the chip demands.&lt;/p&gt;
&lt;p&gt;However, we will use another feature of the 74HC, located on pin 9 (&lt;code&gt;QH&lt;/code&gt; or &amp;ldquo;serial data output&amp;rdquo;). When the 8ths bit is
cascaded and &amp;ldquo;dropped&amp;rdquo; off the chip, it&amp;rsquo;s actually sent out this pin. If the bit is 1, this pin will go high for an
instant, and stay LOW for a 0. This is exactly the kind of input the chip expects on pin 14, &lt;code&gt;SI&lt;/code&gt;! This lets us chain as
many shift registers as we want - &lt;code&gt;QH&lt;/code&gt; of the leading chip to &lt;code&gt;SI&lt;/code&gt; of the following chip, rinse and repeat. We also need
to connect &lt;code&gt;RCK&lt;/code&gt; and &lt;code&gt;SCK&lt;/code&gt; of all the shift registers used, as the signal to read the next bit and display state all
need to happen in sync.&lt;/p&gt;
&lt;p&gt;Since I need 17 bits to control my screen, I&amp;rsquo;ll need to chain 3 shift registers. On a breadboard, that looks a
like this:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://davepedu.com/blog/2015/03/17/arduino-pong-part-1-the-display/img/registers_hu_81a18ebf9f99e469.jpg" alt="Shift register array"/&gt;&lt;/p&gt;
&lt;p&gt;Quick explanation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The dense mixed color wires across the bottom are outputs, as well as the two green wires, and the grey one in
column 57.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The vertical orange wires provide +5v to the chips power input and other pins that need a constant 5v.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The yellow wires ground pins called &amp;ldquo;Enable&amp;rdquo; which literally enable the chip&amp;rsquo;s functionality.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Pin 8 (lower right of each chip) is grounded (not pictured).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The three blue wires in the upper right are &lt;code&gt;SI&lt;/code&gt;, &lt;code&gt;RCK&lt;/code&gt;, and &lt;code&gt;SCK&lt;/code&gt; (left to right). These would go to
our microcontroller.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The horizontal orange wires is connecting &lt;code&gt;QH&lt;/code&gt; of the leading chip to &lt;code&gt;SI&lt;/code&gt; of the following chip (two instances
of these).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The horizontal grey wires connect the &lt;code&gt;RCK&lt;/code&gt; pins of all 3 chips together, and the &lt;code&gt;SCK&lt;/code&gt;s all together.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;We now have the ability to selectively provide power to the 10 rows of our matrix, but what about grounding the 7
columns? We need to be able to translate the 5v signal from 7 of our bits into a some logic: 5v means ground the
corresponding column, and 0v means leaving the column unconnected. A basic way to do this would be a relay. A relay is
like a pushbutton, but to &amp;ldquo;hold&amp;rdquo; the switch on we provide current to the relay, which it forces through a coil to create
a magnetic field pulling two pieces of metal together to complete a circuit.&lt;/p&gt;
&lt;p&gt;However, there are two problems that prevents using a relay for this case: Relays are slow: it physically moves a piece
of metal to connect the circuit. We need to toggle the state hundreds of times per second; this would wear out a relay
very quickly, if one could even keep up. Second problem, relays require higher current to trigger. More than we can
safely provide from the 74HC registers at least. Sure, we could toggle a transistor using our register, and use that to
control a strong power source, but that&amp;rsquo;s unnecessary complication.&lt;/p&gt;
&lt;p&gt;The solution: Darlington pairs. These are simply two transistors connected in a way where we can toggle grounding of one
input by providing voltage to the other. Transistors require much less power than relays, and can act much faster. We
have 7 outputs to control so we&amp;rsquo;d need 14 transistors in 7 pairs. Unfortunately this would take up a large amount of
physical space, at least 21 rows on our breadboard. Too much.&lt;/p&gt;
&lt;p&gt;Enter the ULN2803 (left, pictured above). This chip contains 8 Darlington pairs is one tiny package - perfect! Mine
happens to be a Toshiba brand, but they come in many, many flavors. We simply connect the 7 outputs from our screen to
pins 12-18, and 7 connections from our shift register bank to input pins 1-7. And, most importantly, ground pin 9. Now,
to write some code!&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-c"&gt;#define DS_PIN 2
#define STCP_PIN 10
#define SHCP_PIN 11
#define NUMREG 17
boolean registers[NUMREG];
boolean screen[10][7] = {
{1,1,1,1,1,1,1},
{1,0,0,0,0,0,1},
{1,0,0,0,0,0,1},
{1,0,0,0,0,0,1},
{1,1,1,1,1,1,1},
{1,0,0,0,0,0,0},
{1,0,0,0,0,0,0},
{1,0,0,0,0,0,0},
{1,0,0,0,0,0,0},
{1,0,0,0,0,0,0}
};
void setup() {
// Mark 3 pins for output
pinMode(DS_PIN, OUTPUT);
pinMode(STCP_PIN, OUTPUT);
pinMode(SHCP_PIN, OUTPUT);
}
void writereg() {
// Writes the 17 registers to the shift register bank
digitalWrite(STCP_PIN, LOW);
for(int i=NUMREG-1;i&gt;=0;i--) {
digitalWrite(SHCP_PIN, LOW);
digitalWrite(DS_PIN, registers[i]);
digitalWrite(SHCP_PIN, HIGH);
}
digitalWrite(STCP_PIN, HIGH);
}
void writescreen() {
// Reads the screen array and manipulates regsters[] for each row
// For each column in the row
for(int i=0;i&lt;=10;i++) {
// First, blank the registers
for(int rs=0;rs&lt;17;rs++) registers[rs]=false;
// Mark the column on
registers[9-i]=true;
for(int r=0;r&lt;7;r++) {
// mark any leds on
if(screen[i][r]) registers[r+10]=true;
}
// Push the result to the screen
writereg();
// Leave this frame on for a short time
delayMicroseconds(100);
}
}
void loop() {
// Write one frame to the screen, forever
writescreen();
}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This code is pretty simple too. First, we define which pins are what. Then, create an array of 17 booleans that will be
what we manipulate in the chips memory before pushing it out to the shift registers. Our VRAM, if you will. In my setup,
the first 10 bits represent row state, and the last 7 represent column state. For ease of use, we&amp;rsquo;ll create a 10 by 7
multidimensional array of booleans, 1 to 1 scale with LEDs on our screen. I set it up to show a &amp;ldquo;P&amp;rdquo; (for pong!) by
default, and to confirm all our rows and columns are connected and working.&lt;/p&gt;
&lt;p&gt;In setup(), we tell the arduino which pins are which. Writereg()&amp;rsquo;s purpose is to push our in-memory array of 17 bits out
to the shift registers. Writescreen() calls writereg() once per row of the screen, for one screen &amp;ldquo;refresh&amp;rdquo;. Loop()
calls this forever so we see the P on our screen.&lt;/p&gt;
&lt;p&gt;Bingo!&lt;/p&gt;
&lt;p&gt;&lt;img src="https://davepedu.com/blog/2015/03/17/arduino-pong-part-1-the-display/img/pongp_hu_c4396e5adfaebf3b.jpg" alt="The display showing the letter P"/&gt;&lt;/p&gt;
&lt;p&gt;Coming up in part two: Tracking user input!&lt;/p&gt;</content></item><item><title>nintendo 64 joystick pinout for arduino</title><link>https://davepedu.com/blog/2015/03/11/nintendo-64-joystick-pinout-for-arduino/</link><pubDate>Wed, 11 Mar 2015 00:00:00 +0000</pubDate><author>dave@davepedu.com (dave)</author><guid>https://davepedu.com/blog/2015/03/11/nintendo-64-joystick-pinout-for-arduino/</guid><description>&lt;p&gt;I&amp;rsquo;ve been learning electronics and playing with an &lt;a href="https://arduino.cc/"&gt;Arduino&lt;/a&gt; lately. I realized today I have a
functional, but worn mostly out, Nintendo 64 joystick. For an electronics beginner like me, it would be a fun challenge
to figure out how it works.&lt;/p&gt;
&lt;p&gt;On a side note: I have a spare joystick because I replaced the ones in my Nintendo 64 controllers with a redesigned and
improved replacement stick. The usual getting spots carry such a replacement joystick module for the Nintendo 64. It is
engineered better than the original - it should never wear out!&lt;/p&gt;</description><content>&lt;p&gt;I&amp;rsquo;ve been learning electronics and playing with an &lt;a href="https://arduino.cc/"&gt;Arduino&lt;/a&gt; lately. I realized today I have a
functional, but worn mostly out, Nintendo 64 joystick. For an electronics beginner like me, it would be a fun challenge
to figure out how it works.&lt;/p&gt;
&lt;p&gt;On a side note: I have a spare joystick because I replaced the ones in my Nintendo 64 controllers with a redesigned and
improved replacement stick. The usual getting spots carry such a replacement joystick module for the Nintendo 64. It is
engineered better than the original - it should never wear out!&lt;/p&gt;
&lt;p&gt;Inside of the joystick module, is nice and simple single-sided board. I couldn&amp;rsquo;t conjure up a datasheet from googling
markings on any single component on the inside, so we&amp;rsquo;re on our own! The circuitry inside looked like this:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://davepedu.com/blog/2015/03/11/nintendo-64-joystick-pinout-for-arduino/img/pinout_hu_f640f83e440bec43.png" alt="Joystick PCB with traces"/&gt;&lt;/p&gt;
&lt;p&gt;Simply put, pins 2 and 3 are power and ground (respectively), 1 &amp;amp; 4 are data for left/right, and 5 &amp;amp; 6 for up/down. I
measured approximately 3.3 volts across pins 2 &amp;amp; 3 in a working controller, so we&amp;rsquo;re in luck! The arduino provides a
3.3v rail. I measured that the joystick circuit pulls about 15mA, so we&amp;rsquo;re more than in the clear to power it from the
Arduino. R1 / R2 are 320Ω; I didn&amp;rsquo;t have equipment to measure C1 / C2.&lt;/p&gt;
&lt;p&gt;In the image above, two slotted discs pass through the cutouts near U1 and U2, one for each axis. The movement of the
joystick is geared to each slotted disk, so the precision actually looks pretty high.&lt;/p&gt;
&lt;p&gt;I also don&amp;rsquo;t own an oscilloscope so figuring out the format that the sensors return data could be pretty tricky -
guesswork, really. But again, we&amp;rsquo;re in luck! Someone&amp;rsquo;s created an easy way to &lt;a href="https://www.instructables.com/id/Arduino-Oscilloscope-poor-mans-Oscilloscope/"&gt;use the arduino as a basic
oscilloscope&lt;/a&gt;! Aren&amp;rsquo;t general purpose
processors awesome?&lt;/p&gt;
&lt;p&gt;However, viewing the output from any data pin all looked the same - a flat HIGH or LOW signal when the stick is moving,
and rapid oscilation between high and low during motion. The demo code only supports a single input. Theoretically, it
could be expanded to 6 as the Arduino contains 6 analog inputs, but I&amp;rsquo;m not wanting to dive into that yet. After some
research, I found that the sensors in the joystick appear to be two linear sensors! This kind of sensor uses optics to
track when a slotted medium interrupts some beams of light and encodes the result into two waveforms who&amp;rsquo;s period
indicates speed of motion and offset from eachother determines direction of motion.
&lt;a href="https://www.youtube.com/watch?v=0QLZCfqUeg4"&gt;This video&lt;/a&gt; does a great job of explaining it, and even provides
sample code.&lt;/p&gt;
&lt;p&gt;So, how to use this component with an Arduino? Pins 2 &amp;amp; 3 can be connected directly to the 3.3v rail &amp;amp; ground. 1 &amp;amp; 4 can
go to any digital inputs; 5 &amp;amp; 6 must go to digital inputs that support interrupts.&lt;/p&gt;
&lt;p&gt;I modified code from the video linked above to create the following:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-c"&gt;#define encoderIx 2
#define encoderQx 3
#define encoderIy 18
#define encoderQy 19
volatile int countx;
volatile int county;
void setup() {
Serial.begin(9600);
countx=0;
county=0;
pinMode(encoderIx, INPUT);
pinMode(encoderQx, INPUT);
pinMode(encoderIy, INPUT);
pinMode(encoderQy, INPUT);
attachInterrupt(0, handleEncoderX, CHANGE);
attachInterrupt(5, handleEncoderY, CHANGE);
}
void loop() {
Serial.print("X: ");
Serial.print(countx);
Serial.print(" Y: ");
Serial.println(county);
delay(10);
}
void handleEncoderX() {
if(digitalRead(encoderIx) == digitalRead(encoderQx)) {
countx++;
} else {
countx--;
}
}
void handleEncoderY() {
if(digitalRead(encoderIy) == digitalRead(encoderQy)) {
county++;
} else {
county--;
}
}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;How this works is pretty simple - X and Y are handled separately, and stored in two ints. We&amp;rsquo;ll look at just a single
axis. When the signal to the interrupt-supporting pin is changed, it is compared to the other pin on the same axis. The
joystick&amp;rsquo;s linear encoders express forward/backward as one wave lagging or leading the other - and at the instant the
interrupt function is ran, comparing the interrupt pin to the 2nd input from the same sensor allows us to determine
this. Depending if the other input is high or low, we either increment or decrement the position counter for this axis.&lt;/p&gt;
&lt;p&gt;The sample code above will print the X and Y position of the stick to the arduino&amp;rsquo;s serial console. I used an arduino
mega with the code above; the interrupt-supporting pin numbers might not necessarily be the same for other
arduino models.&lt;/p&gt;
&lt;p&gt;Build anything with this? &lt;a href="https://davepedu.com/contact/"&gt;Let me know&lt;/a&gt;, and I&amp;rsquo;ll post it here.&lt;/p&gt;
&lt;p&gt;Happy hacking!&lt;/p&gt;</content></item></channel></rss>