The next device for my attiny85 to tackle is a tiny EMF detector. So I saw a video from Make magazine on youtube, which then linked me to another random youtuber who had originally done this for their arduino. Essentially the original user had just a single LED which was showing the EMF strength vida LED intensity, the Make guy kind of just improved on that buy using a bar graph and now in the ultimate show of one up-man-ship I decided to move this project over to the ATtiny85 because why waste all those arduino pins.
The code is here and for the most part it’s a copy of the original code
int aOut2 = 0; long EMFval = 0; int averaging = 0 ; int EMFread = 1; int sample = 25; // number of samples per read int sensingRange = 100; // the max expected analog response void setup() { // put your setup code here, to run once: //Serial.begin(9600); }
void loop() { { for (int x =0; x < sample ; x ++) { averaging = analogRead(EMFread)+ averaging; } EMFval = averaging / sample ; //Serial.println (EMFval); EMFval = constrain (EMFval, 0, sensingRange); EMFval = map (EMFval,0,sensingRange,0,255);
analogWrite (aOut2, EMFval); averaging = 0;
}
}
The difficult part clearly wasn’t going to be the code since well I copied the crap out of that. The tricky parts were the pin constraints. Now the hardware required for this project is as follows :
LM3915 bar graph driver
10LEDS or a bar graph (I used 10 leds because I had them lying around.
0.1 uf Cap
10K resister
1K resister
2x100k resister
500K resister
1 push button
Attiny85
5V 1A regulator (if you want to have it fun off batteries or an unregulated supply)
9V (and battery) battery adapter (if you want to have it run on batteries)
and some wire
One you have that you can follow this schematic load up the ATTiny and go on finding some interesting fields
I just realized that I didn’t put a button in the schematic, but anyway it’s trivial pick a spot between the power or ground and connect it, that way when you press the button it’ll power up and you can see the fields. Also I used 400k of resisters because I didn’t have any bigger lying around you can choose bigger or smaller ones based on your needs and just tune the code a bit specifically this variable “sensingRange”.
Here’s a photo of my finished product, I made it a case with my new 3d printer which I will talk about later.
As of yet I don’t have a video of it in action because well I didn’t know how to record it nicely, I would like to walk around home and detect things but then you’d have to view it in shakey cam, so if you want to see it in action click the links above to the original folks who did this and watch their demo, it’s essentially the same, mine is just smaller 🙂
Do you have an ATTiny85 and a shiftbright V2 sitting around doing nothing?! Well you might not but I do.
Well I’ve got a solution to fix that! We can combine the 2 to give you a compact and relatively powerful flashlight! Now why would I want to do this other than because I need to burn parts? Well the shiftbright V2 is an interesting little thing. it’s essentially a 3 colour LED with a controller on it to set power levels and color. So you can’t just connect it to a battery and expect it to power up. It requires instruction! That is where the ATTiny85 comes in, we can use it to blast in some bits to set the LED.
You can find most of the code I used here http://docs.macetech.com/doku.php/shiftbrite to make it . (I’ll post my modified code below) . Also FYI I am coding the ATTiny using the arduino IDE.
The ATTiny85 as I mentioned before has 5 I/O pins (6 if you disable the reset pin) so in this project we’re going to use 4 of them for the shift bright and 1 for a button to toggle between pre-programmed light modes. (RED/WHITE/GREEN/BLUE/STROBE in this case).
The ATTiny85 will be determining how to change the status of the LEDS the way I did it was to time a button hold, to make sure the mode change was deliberate. what I wanted was that if you held the button down for a second but less than 2 seconds it will toggle between red/ white / green / blue, starting at red because that won’t kill your night vision too bad at night. Then if the button is held for more than 2 seconds it goes into a crazy strobe mode, good for when you want to party? I don’t know. To get out of the strobe mode Hold the button for 1-2 seconds and it defaults back to red.
Here’s a small video of it in action!
You can see the schematic below, it’s a pretty simple set up by the way the little 3 pin thing at the bottom is a 5V 1A regulator that I just realized the label is too small on.
And here is the code, again most of it is just a modified version of the shiftbright code in the above link.
int datapin = 3; // DI int latchpin = 2; // LI int enablepin = 1; // EI int clockpin = 0; // CI unsigned long SB_CommandPacket; int SB_CommandMode; int SB_BlueCommand; int SB_RedCommand; int SB_GreenCommand; long previousMillis = 0; long previousMillisStrobe = 0; int buttonMode =0; int interval = 25; int Button1 =0; int Button2 =0; int buttonHold =0; int flashmode=0; int countup=0;
delay(1); // adjustment may be necessary depending on chain length digitalWrite(latchpin,HIGH); // latch data into registers delay(1); // adjustment may be necessary depending on chain length digitalWrite(latchpin,LOW); } void strobe () { // This is the strobe mode, used to just switch colors of the LEDS at 25ms intervals unsigned long currentMillis = millis();
if (currentMillis – previousMillisStrobe > 25) {previousMillisStrobe = currentMillis; countup ++ ; // Count up tells it to switch when it’s incremented
if ( countup ==1 ){ SB_CommandMode = B00; // Write to PWM control registers SB_RedCommand = 0; // Minimum red SB_GreenCommand = 0; // Minimum green SB_BlueCommand = 1023; // Maximum blue SB_SendPacket(); } if (countup ==2) {previousMillisStrobe = currentMillis; SB_CommandMode = B00; // Write to PWM control registers SB_RedCommand = 0; // red SB_GreenCommand = 1023; // green SB_BlueCommand =0; // blue SB_SendPacket(); } if (countup==3) {previousMillisStrobe = currentMillis; SB_CommandMode = B00; // Write to PWM control registers SB_RedCommand = 1023; // red SB_GreenCommand = 0; // green SB_BlueCommand =0; // blue SB_SendPacket(); }
if (countup== 4) {previousMillisStrobe = currentMillis; SB_CommandMode = B00; // Write to PWM control registers SB_RedCommand = 1023; // red SB_GreenCommand = 1023; // green SB_BlueCommand =1023; // blue SB_SendPacket(); } if (countup> 4){if (countup= 0);}} // the strobe mode quickly switch between all the colors then white
} void loop() { unsigned long currentMillis = millis(); Button1 = digitalRead(4); SB_CommandMode = B01; // Write to current control registers SB_RedCommand = 127; // Full current SB_GreenCommand = 127; // Full current SB_BlueCommand = 127; // Full current SB_SendPacket(); if(currentMillis-previousMillis > interval) // Poles the button press at the interval { previousMillis = currentMillis; if (Button1 == 1) {buttonHold++;}// if it was pressed increment the Button hold status if(buttonHold <40 && Button1 == 0) {buttonHold =0;} // if the button was let fo at some point before the 1 second mark reset the button hold status if (buttonHold > 40 && buttonHold <80 && Button1 ==0){flashmode ++;buttonHold =0; }// If it is held between 1-2s then toggle the LED colors if (buttonHold > 80) {flashmode = -1;buttonHold =0;} // If it’s more than 2 seconds switch to strobe mode. if (flashmode ==0) { SB_CommandMode = B00; // Write to PWM control registers SB_RedCommand = 1023; // Maximum red SB_GreenCommand = 0; // Minimum green SB_BlueCommand = 0; // Minimum blue SB_SendPacket(); }
if (flashmode ==1) { SB_CommandMode = B00; // Write to PWM control registers SB_RedCommand = 1023; // Minimum red SB_GreenCommand = 1023; // Maximum green SB_BlueCommand = 1023; // Minimum blue SB_SendPacket();
}
if (flashmode == 2) { SB_CommandMode = B00; // Write to PWM control registers SB_RedCommand = 0; // Minimum red SB_GreenCommand = 1023; // Maximum green SB_BlueCommand = 0; // Minimum blue SB_SendPacket();
}
if (flashmode == 3) { SB_CommandMode = B00; // Write to PWM control registers SB_RedCommand = 0; // Minimum red SB_GreenCommand = 0; // Minimum green SB_BlueCommand = 1023; // Maximum blue SB_SendPacket();
} if (flashmode > 3) { flashmode=0; }
if (flashmode == -1 ) {strobe ();} }
}
Thanks for reading! Heres a couple photos of it.
Oh random fun fact, so from a typical 9V alkaline cell with about 560mAh on it, with this if you run it on white, (using all 3 colors) you should get a good 8 hours of light from it. (60mAh for te LED and 7mAh for the ATTiny)
Before I left to do some training I had bought 25 ATTiny85’s . I still have them and I’m trying to figure out projects for them. So one at a time I will be making projects with them.
For those who don’t know about the ATTiny85 its a 8 pin micro controller. It’s a convenient, solution for very small projects, has a built in 8Mhz clock (with the clock prescaler set to 8, so effectively operating at 1Mhz), can operate with 5V supplies and upto 6 IOs (If the reset pin is disabled, otherwise has 5).
Another thing about these cores is that they can be programmed with the arduino IDE and you can find out more about it here. http://hlt.media.mit.edu/?p=1695