This circuit will introduce 3 new programming concepts that we will expand on in future Project Sets and circuits. The first, new concept is the inclusion and use of libraries.

Transcript

Hey guys it's Christian here from Core Electronics. Today we're going to get started with project set 3, doing circuit 3A. Now project set 3 is all about motion, so we're going to be using something called a server to make motion and in later project sets or in later circuits, we'll be using an ultrasonic range sensor to sense motion.

In this first circuit we're going to be using the servo motor, I highly suggest grabbing some blue tack to pin it down because it is difficult to keep it in place but that blue-tack sure helps. So a servo motor is just a little motor that can be controlled by a pulse width modulation, now of covered pulse width modulation in our previous two project sets in the first one, it was to explain how we could analog write a value to the RGB LED and in our second one, it was to use the tone function so that we could actually write a value to the buzzer so that it could output a frequency or it could output a noise of a given frequency. So, in this the width of the pulse changes where the servo is positioned, now this servo here provided in this set has a range of 180 degrees we are going to limit that between 20 and 160, just so we know that the servo won't break because at its extremities it's usually questionable its integrity. So here we've got one of the tops from the server as you can see and we're essentially just going to pop that on the server and it should slide into the grooves there. So once again blue tack at the bottom of the servo and we're going to place that on the board, now the servo has three female pin headers and what we're going to do is we're going to use these to connect the servo to the red board. So, the red cord is obviously power, so we're going to connect to the positive power rail then we're going to connect the black one to the negative power rail and we're going to connect the white one which is the signal pin or the control pin, we're going to connect that to pin 9 and that's going to be where we control the servo from. Okay so once again in this project set we're going to be using a potentiometer, this potentiometer in this circuit will be controlling the position of the servo, so we're going to plug it in as we do normally the flush by side facing the insulation strip, the middle pin is going to go to A0, the bottom pin is going to go to the ground rail and the positive pin is going to go to the positive power rail, now all we need to do is connect those pins to the power bus, I'm going to use a blue pin for positive power that's going to go to the Volt connection and the ground rail is going to be connected to ground via a black lead. Okay so as we did in project set one I'm going to write this code and step you through it.

So, we can connect that servo there and you might feel that tweak as it initializes, and we'll open an Arduino sketch, so something new to programming in our Arduino programming in this project in this circuit is the use of objects and methods. Now we haven't used those yet, but we will use them for the LCD as well, so to do this we need to include the servo library, so the servo library is a library that contains all the information about our server’s methods. So, a servos methods essentially little functions that are specific to that servo, so we'll be using them to create it, we will create an object called my server. So, we do that by first including the library using this "#include <server.h>", the .h stands for header file because it's the file that contains all the methods for it. So, servo and the object are going to be called "myServo", okay so all that does is it creates an object called my servo and we will learn how to use the methods to write to that later. So, then we're going to call we're going to make a function called "servopin" and we're going to make that Pin 9, because that's where we have connected. Another side value we can use something called Const int, which means that it's a constant integer, once we set it here it can't be changed and usually that's good for pin assignments because it saves a little bit of data. So, from there we've got the servo pin, servo 9 and what we're going to be doing, is we're going to be using our first method in the setup function.

So, in the setup function we're going to write myServo, because that is the object thing “. attached" and what's that saying is we're going to be attaching this server in we're going to be attaching pin 9 or servo pin to the server, so we're saying that the server is connected to pin 9 there, it's similar for this function or this method to say output server in or server pin as an output. Now in the loop what we're going to be doing, is we're going to have an integer and we're going to call it "potValue" and potValue is the analog read of A0, so it's the value at A0 which is what's connected to our potentiometer and we're going to say that, this value is the read in value. Now we're also going to make another integer called servo position or servoPos and that is going to be zero for the time being. Now server pause we're going to redefine here and what we're going to use is we're going to use something called the map function, now what the map function does is it Maps a value from one set of values, to another set of values. So here we want to write the way that the servo write function, or the servo right method works, is we will write an angle to it and it will take that angle. Now as we know from the analog read it has values from 0 to 1023, now if we write a value of 1023 it's going to push the servo past 180 and it's going to try and keep going and that will break the servo, so what we want to do is we want to say map we're going to write what we're mapping which is plotValue, so we're mapping potValue we're going to put in what our old or potValue values are, so at the extremities so we're going 0 and 1023 and what we want our new ones to be. So, as I said before we don't want to push the server to his extremities, so we're going to be keeping it from 20 degrees to 160 degrees, so write 20 and then 160 and then that's our map function. Now what that's going to do is it's going to change the value from whatever it is in there and it's going to scale it in such a way that it will fit within these belts.

So, from there we use our next method, we're going to say "myServo. Write and what we're going to write to it is servoPos. Now if we want to see what the value of servoPos. is at a given time, what we can also do is in this setup function we can start our serial monitor so, serial. Begin and we're going to start it off with 9600 baud rates. Then from here we're going to go serial.Prints and we're going to print servoPos. Now another cool thing about the serial monitor is that we have a different method or "println", which will create a new line underneath it and that way we won't just have it going across the screen, we will have it going down the screen and that's a lot easier to read.

So now that that's done we will upload that code to the Red board and we should see the upload work hopefully. Now it's done uploading, so if we want to have a look in the serial monitor here we can just click on that and we're at currently a hundred and eleven. So, if we change this you'll notice that the servo is changing its position there. Okay and you can see here on the serial monitor, that as we change this it goes from twenty at one extremity, to a hundred and sixty or 159 at the other extremity and as we change this it's being mapped to this server. So that is exactly what we wanted to control the server, so in this circuit we learnt how to include the server header library or the server library, we learnt how to create an object, we learnt how to use constant integers for pin assignments, so that we save more data, as opposed to just normal integers, we learned how to initialize the server, initialize the serial monitor, we learnt how to use the map function, so that we can remap or rescale values and we learnt how to print with a new line to the serial monitor and how to ultimately write to the server.

So that has been circuit 3A for project set 3. Hope you enjoyed it, in the next one we will be using an ultrasonic rangefinder, to make a LED light up.

Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.