Thursday, May 30, 2013

Stepper Motors and Arduino: 28BYJ-48 with ULN2003

Today I will be exploring the world of stepper motors. I recently purchased a 28BYJ-48 stepper motor with a ULN2003 controller. They are available from a host of vendors for a few dollars and seem to be pretty popular in the Arduino community.

Getting started, there are several links you might find useful.

  • Basic information on the motor and controller as well as a sample sketch using the standard Stepper library.
  • The Stepper Library- This is the library that is included with the Arduino IDE. This library is set up to run a stepper without a gearbox, so it would have to be modified.
  • Stepper2.ino- This sketch includes a full set of functions that can be used to run the 28BYJ-48. It is discussed on THIS page, but it appears that the plans to convert it into a "real" library were never implemented. 
  • Custom Stepper Library- This library can be used to control a variety of steppers, but the default settings are for the 28BYJ-48
First I just wanted to get the stepper turning. I found THIS forum thread with some basic code to get it running. HERE is the code. It is very basic and does work. If you read THESE notes and still didn't understand how steppers work. This sketch might clear it up for you. Below is a video of the sketch working. 

Note how it is wired. You don't want to power the stepper from the Arduino. It can pull 90mA which is a lot for your little Arduino. I used my nifty breadboard power supply that I picked up for a few dollars on Ebay. Power goes to the left 2 male pins on the ULN2003 breakout (marked - + 5-12V). The jumper on the right just switches power to the motor. Removing it opens the circuit between the + power supply and the motor. Other than that, just use some female-female jumpers to connect the inputs to the whatever pins you are using on the Arduino. I am using an Arduino Mega 2560 with an Arduino sensor shield v4, so this is very easy to do. For those that don't know, the ULN2003 is just a little Darlington Array that allows us to switch power from an external source on and off rather than using the Arduino's on board power supply.

Next I decided that I would try the Arduino Stepper library first. While Stepper2 looks promising, I wanted something actively supported. Luckily THIS wiki provides code for using the standard library. HERE it is again, saved for posterity. I will note that 4096 steps resulted in 2 revolutions. Also, at the default steps/revolution 300 appeared to be a good maximum speed. 400 would not run at all. When I changed the steps/revolution to 2048, 10 worked well as a max. Another useful thing to know, the clockwise and counter-clockwise directions are defined when looking at the motor from the back (the side with the label). That is, from the perspective I used in the video above it will be backwards. 

If it is not working try some of the things below. If those don't help, Google the problem. If all else fails, comment, and I will see what I can do.
  • Reduce the speed. These motors only turn so fast before they bind up and stop moving.
  • Check the motor's temperature. The top speed of mine seemed to depend a bit on how warm it was.
  • Check wiring. Make sure your Arduino is hooked up correctly and you have defined the right pins in your code.
  • Check the jumper on the ULN2003 control board. It must be in place (bridging the right two pins).
Well that's about all I have at the moment. I have not dug into the other two stepper libraries I listed. I just wanted to do an intro so I could add stepper motors to my robo-arsenal. If I do any projects with them I will post about it.


Tuesday, May 28, 2013

nRF24L01+ Arduino Communication on Arduino Mega 2560

Disclaimer: Note the last paragraph.

In my quest to explore inter-Arduino communications, I bought 2 nRF24L01+ modules. These are pretty neat radios. From what I have read, they are AM. They can be used with key fob remotes or in a network of up to 6 modules. They are also very cheap. My 2 were $3 on Ebay, but many vendors sell them.

When I first began working with these modules I needed a way to interface with them. Wanting to
breadboard at least one of them, I created the adapter shown to the right. It isn't a perfect solution. The module itself gets in the way of wiring slightly, but my jumpers fit in there good enough.

Next I loaded the RF24 Library. HERE is a blog post by maniacbug that details using the module on an Arduino Uno. It is very useful. THIS page also helped. However, I don't have an Uno. I only have an Arduino Mega 2560. This means that we need to change a few things.

First, we need to change the pins. The Arduino Mega's SPI pins are in different positions than the Arduino Uno. You can figure these out pretty well or you can look below.
          Uno           Mega

  • 11       -      51            (MOSI)
  • 12       -      50            (MISO)
  • 13       -      52            (SCK)
  • 10       -      53            (CSN)
  • 9         -      40 (Your choice) (CE)
Another note, IRQ is not needed for anything I will be discussing. Just leave it unplugged. 

Second, the code needs to be changed slightly.
RF24 radio(9,10);        needs to be changed to      RF24 radio (40, 53);

On the receiving end, I decided to use my Hackduino. Since it is basically an Arduino Uno, the pins are wired the same, and the example code does not need to be changed at all.

However, I did have the problem of needing a 3.3V power supply. Well a few minutes and a Google search later I found THIS calculator and built my first voltage divider circuit. I used a 470 Ohm resistor between 5v and output. 220 and 22 Ohm resistors in series (for a total of 242 Ohms) were placed between output and ground. I read it with a volt meter and it was right on the money, 3v3.
3.3v voltage divider
Black: GND   White: 5v    Green: 3.3v output

Well here goes nothing. Open the serial monitor and type t. I got this screen.

The first part of that is fine. The second is not.

Well I did some digging. While I had found two forum threads (HERE and HERE), I had not found them terribly useful. They came to the conclusion that he Arduino Mega power supply was the problem. While this may be the case, no combination of capadcitors seemed to fix it. I also went on to try the power supply from my Hackduino, 2 AA batteries, and the 3.3V supply on my breadboard power supply. None of these things worked.

I did successfully get it to send once, but I have no idea how. It just worked. I unplugged the USB and plugged it back in, and it didn't work. I have rung out every jumper I am using and have tested all the pins with an LED. I swapped my two RF modules out. Same problems. At this point, I really have no clue what the problem is. I don't have an Arduino other than this one to try it on. I also don't have a variable power supply to test with.

Well that's all I have. I admit that I am quite disappointed with myself for posting an unsuccessful project. I don't know if I will continue working on it or not, but if I do get it working I will post an update. I may try the Mirf library, but we will see. I only paid a few dollars for these modules, and if they are bad I don't want to waste any more time on it. If anyone has any suggestions, feel free to comment. Regardless of my apparent failure, I still learned quite a bit in this endeavor. I hope you have better luck!


Monday, May 20, 2013

Virus Removal: Part 2

Well this is part two, which is unfortunate. As I feared might be the case, we did not get all of the viruses on my relative's computer (see part 1). The next morning after I turned controls back over to my relative, we got a call. Something called Amazon Cloud Drive came up.

Well I can handle this, get in the car and drive over. Turns out Amazon Cloud Drive is not the problem, not even close. Some "mysterious" entity had broken the internet and the sound drivers. More snooping, the Windows Security Service that handles firewalls and such things (I gathered that is what it did) would not launch. Things were going haywire. Well fantastic.

Not having time to deal with it just then I kicked off a virus scan and left. On the way home I made a quick call to my brother. Though it is not his field, he has more experience at virus hunting than I do. He was quite encouraging (NOT). Basically, he suggested the problem was what I had already suspected but had not wanted to concede. He decided it was likely that she had a rootkit.

Well that is bad news. Malwarebytes had "removed" rootkit.0Access the day before, but at this point I felt pretty stupid for believing I had beaten it. His suggestion, reformat the drive with an external program and rebuild the computer from the ground up. "Scorched earth." Right. Well I had not conceded that fully yet.

1. Run Malwarebytes Anti-rootkit BETA. I don't work for them. I promise. I just happened to have seen this feature in the previous days and tried it ( from safe mode). It found a rootkit. I "removed" it. Yeah. Right.

2. Run Bitdefender Rescue CD. Well the Trend Micro rescue disk didn't find it last time, but we'll try it. It booted from Linux and scanned the drive. Interestingly, the internet works when booting from Linux. Yep. Virus.

3. Restore System. After much deliberation it was decided that we should restore the system. This basically resets the computer to an image on a hidden partition of the hard drive. Even if we got the virus, the internet and sound drivers as well as who knows what else were hosed. Yes we probably could fix it, but all she does is use it for email and news. We'll just restore it.

We tried to do this through Windows. No dice. Next, reboot and hit alt-f10 to do it outside of Windows. This took several tries for some reason, but eventually we had success. We did the one that saved her documents. For those that are wondering how long this will take (like we were), it took over an hour.

4. Reboot and reassess  Everything that looks good. Reinstall all the antivirus software. Scan the Backup folder where all her old documents were stored. It found something. Delete that and scan again. Then full system scan.

Now we put the computer back together again and hope for the best. Here's to hoping there is no part 3.


Friday, May 17, 2013

FBI Virus Removal

This is a little off topic, but I thought it might be useful nonetheless.

I recently had the pleasure of playing computer doctor for an older relative of mine. Somehow her computer (Windows 7- 64 bit) had acquired a lovely case of the FBI virus. On start-up, the computer was immediately hijacked and a page was loaded in place of Windows Explorer. It told the user that the FBI had taken over the computer that you are responsible for paying a fine of varying amounts for breaking various laws. It even took over the computer's webcam and took a picture. Fun stuff.

Google "FBI Virus" and you will get all sorts of information of the various strains. I don't know what the official name of the strain on this computer was, but these are the steps I went through to eliminate it. Note that the first few did not work (at least for me).

1. Boot in safe mode. This did not work. While the FBI ransom screen did not come up, as soon as Windows Explorer was launched, the computer would restart.

2. Boot in safe mode with command prompt. This did work. From here you can launch windows explorer by typing "explorer" (or I suppose any other program if you know the correct prompts). From this I was able to load Malwarebytes, but it needed to download definitions. Well safe mode with networking does not work. This means I would have to use the 40 day old definitions (see step 7). No good I decided. It's also worth noting that the anti-virus on the computer would not load at all, even in safe mode.

3. Trend Micro Rescue Disk (via USB)- This computer had Trend Micro Titanium on it when it was infected, so I decided to try the Rescue Disk. This boots the computer off of the flash drive (on Linux I think) and scans it from there. 3 hours later, no threats found. Next method.

4. msconfig. I should have tried this first, but didn't think of it. Booting in safe mode with command prompt I was able to launch msconfig (type msconfig in the command line). I then shut off all startup programs. I don't know if this is any different than booting in safe mode or not. I suspect not because it did not work.

5. HitmanPro. I ran loaded HitmanPro and tried scanning with that. It found some cookies and skype.dat. Well a google later and skype.dat was quarantined. Apparently that might be a virus as well. If I kill her skype I can fix that later (it didn't kill skype). I will also say that I had never heard of HitmanPro before. I just read that someone had had success with it and tried it. I had used Malwarebytes in the past.

6. Remove Adobe Flash. I read that the virus might depend on Flash to run, so I uninstalled every Adobe Product I could get my mouse on.

Beginnings of Results
7. Update Malwarebytes definitions manually. You can download newer definitions from HERE. I say newer because it said they were 8 day old. I copied them over with a flash drive and installed them. Ran a quick scan (all with explorer launch from safe mode with command prompt). It found skype.dat and its registry file. Malwarebytes actually deleted it. Restart in safe mode with networking. It works now. Download latest Malewarebytes definitions and run full scan. I did not reboot the computer normally at this point because I feared that the computer had more than one virus on-board.  If one of them survived, it could have reloaded the other ones (such was the fear anyway).

The full scan removed three more vicious looking threats Malware.Packer.CV, Rootkit.0Access, and Rootkit.0Access. I rebooted into safe mode again.

8. Run Microsoft Security Scanner. Downloaded and scanned. It found nothing.

I restarted the computer normally and everything appears normal. Now run more scans to make sure everything is OK and re-install anti virus software. The program files folder for Trend Micro was vacant, so  I installed Microsoft Security Essentials because it was free and didn't require a licence key. Re-install all the "needed" Adobe products. Reset all startup programs. I also noted that some of the internet security settings on IE appeared to be changed. Unchange them. For good measure, install ccleaner and clean up cookies, registry, and any other junk. Turn on daily virus scans and Windows automatic updates. Cross fingers and hand keys back to relative.

Good luck!

List of references:

Other things to try:

  • Kaspersky Rescue Disk
  • System Restore Point
  • Manually boot OS from flash drive
  • Hire a teenager
  • Buy a new computer and send me the broken one as a gift 

Monday, May 13, 2013

Arduino: I2C Communication between 2 Arduinos with Wire Library

The next method of communication I will look at is I2C communication. I2C stands for Inter-integrated circuit. It allows one master device to connect to a large number of slave devices using only 2 pins (signal and clock).  The nice folks at Arduino have made this very easy with the Wire library. It is included with the Arduino IDE and is supported on most Arduino boards.

Before we begin you may want to peruse the Arduino website. The Wire library is described HERE and detailed HERE.  When you finish those, let's dive in.

The first thing I did was run the master_reader and slave_sender examples. This was very easy. It sends hello from the slave to the master and displays it on the serial monitor.

Below is a picture of the wiring. It is simple. Wire the SCL pins and the SDA pins together through a pull-up resistor. I used 4k7. A good example can be found at THIS instructable. You also need to connect the grounds of the two boards as well as the vcc. I copied over the diagram for convenience (to the right).

Next I decided to make it transmit something useful. I made an I2C HC-SR04. The slave Arduino constantly takes distance measurements with the HC-SR04 sonar module. The master Arduino can then poll the slave whenever it wants to get the current distance measurement.

Wiring is the same as above. Make sure you use your pull-up resistors. I connected the trigger pin of the sonar to pin 7 and the echo to pin 8, but you could use any of them.

HERE is my code. Hopefully I will have time to rewrite the slave for the ATtiny in the future. That is the plan. Right after wireless communication, robot arm, computer vision..


Saturday, May 11, 2013

Arduino: Serial Communication Between Two Arduinos

I wanted to know more about inter-Arduino communication, so I did this project. I want to explore the different ways to communicate with and between Arduinos.

First up is serial communication. This is pretty simple. I expected there to be more hitches, but it turned out to be pretty straight forward. Well let's get to it.

First things first, HERE is the Arduino playground page for serial communication. You might want to read up on the different commands you have there.

Ok, first program. One Arduino transmits one integer every second counting from 1 to 10. The receiving Arduino then prints that number to the COM port where I can see it.  I only have one Arduino with a working USB to serial interface, so I used it as the Rx.

The code is simple enough. You can get mine HERE, but I would encourage anyone to try to write it yourself. It helps, and this is short enough. The only unexpected detail is the parseInt(). I first tried a simple read(). Well that doesn't work because that displays the ASCII character and not the integer value. I had forgotten that.

To wire it up, simply connect Tx (probably pin 1) of the Tx board to the Rx (probably pin 0) of the Rx board. Then connect a common ground between the two. Note that you may need to connect these pins after you load the program on the Arduino (since you are using that pin to communicate with the computer via the USB to serial chip). Leaving them connected caused an error for me. This might be avoided if you put a resistor in between the 2 boards, but I have not tried. By the way, if you're wondering where I got that super cool Tx Arduino, check out THIS post. It was really cheap fun!

Left: Rx                         Right: Tx
Well that was cool. Now I want to go the other way. The next program blinks the LED on the Rx board a specific number of times entered through the Serial Monitor connected to the Tx board.

The code again is not that difficult. HERE is mine. Again, use parseInt() and you're golden.

The wiring is equally simple. It's the same as you just did except in reverse the two boards. In fact, if you had two "real" Arduinos with an FTDI or similar, you wouldn't even need to do that. Just make sure you wire in the common ground as always.

Left: Tx                       Right: Rx

Well that's great and all, but I want to communicate both ways. Next program. This one takes an integer value from a user input through the Serial Monitor and then sends it to the Rx Arduino. The Rx then adds 5 to the integer an sends it back to the Tx. Note that the Tx and Rx labels are a bit arbitrary in this one. It's more of a master-slave relationship.

HERE is my code if you want it.

Wiring is similar to above. However, I used Serial1 on the Tx to communicate with the Rx board. This just kept the USB communication from interfering. This functionality is only available on the Arduino Mega as far as I know (besides 3rd party boards). See the Arduino Documentation linked at the beginning of this post for more details. It's also interesting to note that I noticed I didn't need to power the Rx board for it to work. It would draw power from the Tx pin. That was interesting. I powered it anyway.

Ok.  Last program. This one only uses one Arduino. It allows you to input a word , and it mirrors it back to you. Now this is not all that impressive in itself, but it is still very useful.

There are only a few major changes to the code we have been using. We need to make the variables we use char variables. Also we need to change the parseInt() to a plain read(). Another note, remember that serial is one bit at a time. So if you want to do a line return, you need to handle that yourself. I made it do one every time it saw a period. \n would be more traditional, but that was more work.

HERE is my program. It is pretty simple, but I am not a programmer. If your program is doing anything else you will probably want to use a char string. THIS looks like a good example of this.

Wiring nonexistent. Just connect your Arduino via USB.

That's all I have for now. Hope this was useful! If you want the full package of programs you can get them in zip HERE. Also, if you want to learn about serial communication with an ATtiny (a $2 microcontroller) check that out HERE. If you are looking for something else, try my communication label. There may be something in there that will interest you.