Friday, June 30, 2017

Median vs. Average, Arduino Calculations

If we want to average a set of numbers, we add them together, then divide by the number in the set.

150 + 200 + 0 = 350 / 3 = 116 Average

I'm using int and dropping the fractional parts. Use float if you need them.

But if we want the Median, which is the center, or middle number, we have to sort and compare each number to the others to get a list from high to low, and grab the center number. In this case, the Median is 150.

I've written a sketch that calculates both so you can compare your own numbers, and make it a much larger list:

int a[] = {150,200,0};
int n = 3;

a[] is your set of values

n is the number of values in the set

https://pastebin.com/Ge0pZGS2

More info on Mean, Median & Average - https://www.vocabulary.com/articles/chooseyourwords/mean-median-average/





Thursday, June 29, 2017

6 Channel Infra Red Transceiver Sensor Board

Calling all Model Railroaders (ok, it's not just for Model Railroading)! Remember our Scale Speedometer and Crossing Light project? We used IR transceivers in the track to detect a train passing overhead. Perfect for block detection and other projects. We are releasing a 6 channel IR sensor board (fully populated) for use with Arduino, Raspberry Pi, PIC and other microcontrollers. Comes with all components, including the 6 IR transceivers wired to 6' cords. All connections are screw terminals for ease of use. Each phototransistor has a potentiometer for adjusting sensitivity. Operates at 5v or 3.3v (select which version). Comes with sample code for Arduino.

$10 Pre-release Discount! 

Price goes back to $55 when the initial production run is exhausted, so order early!





Voltage Options

Sunday, June 25, 2017

Transistor Sizing Calculation

Two common ways of using a transistor is as an analog amplifier, or a digital switch. We are most interested in the digital switch mode of a transistor in our projects.

Many times, an Arduino, or other microcontroller, is not able to drive a load directly. We commonly will use a transistor to drive the load, and trigger it with a microcontroller's output pin.

For MOSFET examples, see our companion article.

Lets use an example. Say we have a 20v dc motor, that requires 500ma of current at load.

The Arduino UNO can output 5v, at 40ma or less. Let's use a 2N2222 transistor. From the spec sheet, we see it can handle 600ma continuously, so current requirements are met. We also see it can handle around 30vdc, so the voltage requirements are met. Looks like a good match.

Being a NPN transistor, the load attaches between the collector and the positive supply, the emitter connects to a common ground with the Arduino.

To prevent the transistor from pulling too much current from the Arduino, we need a resistor between the Arduino output pin, and the base of the transistor. The resistor must be sized to limit current to no more than 40ma, and to make sure the transistor "opens" fully under load. The more current drawn by the load, the more current is needed by the base.

Download spreadsheet with following calculations:

The 2N2222 has a DC gain of about 30, so the current of the load (500 ma) divided by the gain (30) means we need a base current of around 16ma.

If we assume a Arduino HIGH being around 4.5v, and the diode voltage of the transistor is 0.7v, we get a base voltage of 3.8v. The maximum size of the resistor to maintain a 500ma load will be 228 Ohms. We found this by taking the needed base current of 16ma and dividing by the base voltage of 3.8v (and multiplying by 1000). If you go higher, the transistor may not fully open, and the motor won't perform properly. We could go as low at 100 Ohms without exceeding the max current of the Arduino pin (3.8v / 100 Ohms = 38ma), but there's no need to draw the excess current, so keep the resistor value near the max of 228 Ohms to reduce Arduino power consumption, and leave capacity on other pins. The total current supply capability of the UNO is 200ma (per ground) across all the pins.