Cardiac pacemaker
The contraction of cardiac muscle (heart muscle) in all animals is initiated by electrical impulses known as action potentials. The rate at which these impulses fire controls the rate of cardiac contraction, that is, the heart rate. The cells that create these rhythmic impulses, setting the pace for blood pumping, are called pacemaker cells, and they directly control the heart rate. They make up the cardiac pacemaker, that is, the natural pacemaker of the heart. In most humans, the concentration of pacemaker cells in the sinoatrial (SA) node is the natural pacemaker, and the resultant rhythm is a  sinus rhythm. Sometimes an ectopic pacemaker sets the pace, if the SA node is damaged or if the electrical conduction system of the heart has problems. Cardiac arrhythmias can cause heart block, in which the contractions lose any useful rhythm. In humans, and sometimes in other animals, a mechanical device called an artificial pacemaker (or simply "pacemaker") may be used after damage to the body's intrinsic conduction system to produce these impulses synthetically. Control. Primary (SA node). One percent of the cardiomyocytes in the myocardium possess the ability to generate electrical impulses (or action potentials) spontaneously. A specialized portion  of the heart, called the sinoatrial node (SA node), is responsible for atrial propagation of this potential. The sinoatrial node (SA node) is a group of cells positioned on the wall of the right atrium, near the entrance of the superior vena cava. These cells are modified cardiomyocytes. They possess rudimentary contractile filaments, but contract relatively weakly compared to the cardiac contractile cells. The pacemaker cells are connected to neighboring contractile cells via gap junctions, which enable them to locally depolarize adjacent cells. Gap junctions allow the passage of positive cations from the depolarization of the pacemaker cell to adjacent  contractile cells. This starts the depolarization and eventual action potential in contractile cells. Having cardiomyocytes connected via gap junctions allow all contractile cells of the heart to act in a coordinated fashion and contract as a unit. All the while being in sync with the pacemaker cells; this is the property that allows the pacemaker cells to control contraction in all other cardiomyocytes. Cells in the SA node spontaneously depolarize, ultimately resulting in contraction, approximately 100 times per minute. This native rate is constantly modified by the activity of sympathetic and parasympathetic nerve fibers via the autonomic nervous system, so  that the average resting cardiac rate in adult humans is about 70 beats per minute. Because the sinoatrial node is responsible for the rest of the heart's electrical activity, it is sometimes called the "primary pacemaker". Secondary (AV junction and Bundle of His). If the SA node does not function properly and is unable to control the heart rate, a group of cells further down the heart will become the ectopic pacemaker of the heart. These cells form the Atrioventricular node (or AV node), which is an area between the left atrium and the right ventricle within the atrial  septum, will take over the pacemaker responsibility. The cells of the AV node normally discharge at about 40-60 beats per minute, and are called the "secondary pacemaker". Further down the electrical conducting system of the heart is the Bundle of His. The left and right branches of this bundle, and the Purkinje fibers, will also produce a spontaneous action potential at a rate of 30-40 beats per minute, so if the SA and AV node both fail to function, these cells can become pacemakers. It is important to realize that these cells will be initiating action potentials and  contraction at a much lower rate than the primary or secondary pacemaker cells. The SA node controls the rate of contraction for the entire heart muscle because its cells have the quickest rate of spontaneous depolarization, thus they initiate action potentials the quickest. The action potential generated by the SA node passes down the electrical conduction system of the heart, and depolarizes the other potential pacemaker cells (AV node) to initiate action potentials before these other cells have had a chance to generate their own spontaneous action potential, thus they contract and propagate electrical impulses to the pace set by  the cells of the SA node. This is the normal conduction of electrical activity in the heart. Generation of action potentials. There are 3 main stages in the generation of an action potential in a pacemaker cell. Since the stages are analogous to contraction of cardiac muscle cells, they have the same naming system. This can lead to some confusion. There is no phase 1 or 2, just phases 0, 3, and 4. Phase 4 - Pacemaker potential. The key to the rhythmic firing of pacemaker cells is that, unlike other neurons in the body, these cells will slowly depolarize by  themselves and do not need any outside innervation from the autonomic nervous system to fire action potentials. In all other cells, the resting potential (-60mV to -70mV) is caused by a continuous outflow or "leak" of potassium ions through ion channel proteins in the membrane that surrounds the cells. However, in pacemaker cells, this potassium permeability (efflux) decreases as time goes on, causing a slow depolarization. In addition, there is a slow, continuous inward flow of sodium, called the "funny" or pacemaker current. These two relative ion concentration changes slowly depolarize (make more positive) the inside membrane potential (voltage) of  the cell, giving these cells their pacemaker potential. When the membrane potential gets depolarized to about -40mV it has reached threshold (cells enter phase 0), allowing an action potential to be generated. Phase 0 - Upstroke. Though much faster than the depolarization of phase 4, the upstroke in a pacemaker cell is slow compared to that in an axon. The SA and AV node do not have fast sodium channels like neurons, and the depolarization is mainly caused by a slow influx of calcium ions. (The funny current also increases). Calcium enters the cell via voltage-sensitive calcium channels that open  when the threshold is reached. This calcium influx produces the rising phase of the action potential, which results in the reversal of membrane potential to a peak of about +10mV. It is important to note that intracellular calcium causes muscular contraction in contractile cells, and is the effector ion. In heart pacemaker cells, phase 0 depends on the activation of L-type calcium channels instead of the activation of voltage-gated fast sodium channels, which are responsible for initiating action potentials in contractile (non-pacemaker) cells. For this reason, the pacemaker action potential rising phase slope is more gradual than  that of the contractile cell (image 2). Phase 3 - Repolarization. The reversal of membrane potential triggers the opening of potassium leak channels, resulting in the rapid loss of potassium ions from the inside of the cell, causing repolarization (V gets more negative). The calcium channels are also inactivated soon after they open. In addition, as sodium channels become inactivated, sodium permeability into the cell is decreased. These ion concentration changes slowly repolarize the cell to resting membrane potential (-60mV). Another important note at this phase is that ionic pumps restore ion concentrations to pre-action potential status. The sodium-calcium  exchanger ionic pump works to pump calcium out of the intracellular space, thus effectively relaxing the cell. The sodium/potassium pump restores ion concentrations of sodium and potassium ions by pumping sodium out of the cell and pumping (exchanging) potassium into the cell. Restoring these ion concentrations is vital because it enables the cell to reset itself and enables it to repeat the process of spontaneous depolarization leading to activation of an action potential. Clinical significance. Damage to the SA node. If the SA node does not function, or the impulse generated in the SA node is blocked before it  travels down the electrical conduction system, a group of cells further down the heart will become its pacemaker. This center is typically represented by cells inside the atrioventricular node (AV node), which is an area between the atria and ventricles, within the atrial septum. If the AV node also fails, Purkinje fibers are occasionally capable of acting as the default or "escape" pacemaker. The reason Purkinje cells do not normally control the heart rate is that they generate action potentials at a lower frequency than the AV or SA nodes.