Arduino Tips and Tricks

I will be compiling some useful tips for you guys in this page to help with your Arduino projects.

A faster way to read and write to / from digital pins.

Have you noticed how slow reading and writing digitals pins is with the:

digitalRead();

and

digitalWrite();

functions?

Well I sure have (and upon searching the internet, other people have as well.)

So, if you are building a project that requires faster read and write speeds than what you normally can get using the above functions, you should instead try using:

bitRead();

and

bitWrite();

functions.

They require just a little more knowledge of the specific Atmel microcontroller that is at the heart of your Arduino board, but it’s pretty simple to work out. For example, if you wanted to send a logic 1 (high) to digital pin13, you would normally do it like this:

digitalWrite(13, HIGH);

The Arduino IDE knows where digital pin 13 is connected on the microcontroller. So if you wanted to use the bitWrite function on digital pin 13, you first have to find out the PORT and pin number of that port, that digital 13 belongs to. To do this, we need the Arduino schematic (here’s a zoomed in area of the Arduino Nano containing a 328p microcontroller):
328pZoomedIn

The capital D’s represent the digital pins. We want digital pin 13 ‘D13’ this happens to be connected to PORTB pin 5 (PB5). So in order to send a logic 1 to digital pin 13 – using the bit Write function, we do it likes this:

bitWrite(PORTB,5,1);

In the brackets we first write in the port we’re concerned with, then the pin number of that port and then finally, we write in what we want that port pin to be (I.E. either a 1 or a 0 – in this case, it’s a 1).

And that’s it for the first tip, you will find that the speed of reading and writing pins is MUCH quicker with the bitRead and bitWrite methods compared to the standard digitalRead and digitalWrite methods.

 

More to come as I think of them.

 

 

 

0 0 votes
Article Rating
Subscribe
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

6 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

hello sir its good to see u here.
sir i want to send an image from the pc to arduino ram. can u help me?
remember i am using arduino due. sir can u guide me or refer me somelink which can be helpful for me:)
thanks in advance:)

Hi bilal, you are going to need to give me more information. what resolution is the image? What is the color depth (how many bits) what are you trying to display the image on (and LED matrix maybe)?

hello sir
I am trying to store the image in the arduino ram & then send the commands from arduino to print that image. Resolution & color depth can be of any value.

Thanks

Hi Brad, I got put onto you from a friend who was in the RAAF with you back in the day. Im doing my final thesis project for University, creating a data logging device which logs data received by accelerometers. Ive run a few experiments with the device using several analogue sensors, which gave a result but it wasnt that tangible. https://learn.adafruit.com/adxl345-digital-accelerometer/programming Just need a hand getting the best out of my programming, few little things I’m having an issue with as this accelerometer communicates over SDA SCL (little different to the previous analogue ones Ive been using). I also… Read more »

Hi Brad,
I subscribed to your You Tube instructional video’s then found this link.
I’m a motorcycle mechanic but can’t do that any longer so I’m trying to learn some electronics. I was working as an instructor but got hurt doing demo’s for students, (no, I didn’t crash anything LOL)
Are you a teacher or and engineer (or both?)
Anyway, thanks for taking the time to make vids

PJ

16
0
Would love your thoughts, please comment.x
()
x